forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.c
More file actions
210 lines (173 loc) · 5.63 KB
/
notifier.c
File metadata and controls
210 lines (173 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
#include <sof/common.h>
#include <rtos/panic.h>
#include <rtos/idc.h>
#include <rtos/interrupt.h>
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/cpu.h>
#include <sof/lib/memory.h>
#include <sof/lib/notifier.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <rtos/sof.h>
#include <ipc/topology.h>
#include <stdint.h>
LOG_MODULE_REGISTER(notifier, CONFIG_SOF_LOG_LEVEL);
/* 1fb15a7a-83cd-4c2e-8b32-4da1b2adeeaf */
DECLARE_SOF_UUID("notifier", notifier_uuid, 0x1fb15a7a, 0x83cd, 0x4c2e,
0x8b, 0x32, 0x4d, 0xa1, 0xb2, 0xad, 0xee, 0xaf);
DECLARE_TR_CTX(nt_tr, SOF_UUID(notifier_uuid), LOG_LEVEL_INFO);
static SHARED_DATA struct notify_data notify_data_shared[CONFIG_CORE_COUNT];
struct callback_handle {
void *receiver;
void *caller;
void (*cb)(void *arg, enum notify_id, void *data);
struct list_item list;
uint32_t num_registrations;
};
int notifier_register(void *receiver, void *caller, enum notify_id type,
void (*cb)(void *arg, enum notify_id type, void *data),
uint32_t flags)
{
struct notify *notify = *arch_notify_get();
struct callback_handle *handle;
k_spinlock_key_t key;
int ret = 0;
assert(type >= NOTIFIER_ID_CPU_FREQ && type < NOTIFIER_ID_COUNT);
key = k_spin_lock(¬ify->lock);
/* Find already registered event of this type */
if (flags & NOTIFIER_FLAG_AGGREGATE &&
!list_is_empty(¬ify->list[type])) {
handle = container_of((¬ify->list[type])->next,
struct callback_handle, list);
handle->num_registrations++;
goto out;
}
handle = rzalloc(SOF_MEM_ZONE_SYS_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(*handle));
if (!handle) {
tr_err(&nt_tr, "notifier_register(): callback handle allocation failed.");
ret = -ENOMEM;
goto out;
}
handle->receiver = receiver;
handle->caller = caller;
handle->cb = cb;
handle->num_registrations = 1;
list_item_prepend(&handle->list, ¬ify->list[type]);
out:
k_spin_unlock(¬ify->lock, key);
return ret;
}
void notifier_unregister(void *receiver, void *caller, enum notify_id type)
{
struct notify *notify = *arch_notify_get();
struct list_item *wlist;
struct list_item *tlist;
struct callback_handle *handle;
k_spinlock_key_t key;
assert(type >= NOTIFIER_ID_CPU_FREQ && type < NOTIFIER_ID_COUNT);
key = k_spin_lock(¬ify->lock);
/*
* Unregister all matching callbacks
* If receiver is NULL, unregister all callbacks with matching callers
* If caller is NULL, unregister all callbacks with matching receivers
*
* Event producer might force unregister all receivers by passing
* receiver NULL
* Event consumer might unregister from all callers by passing caller
* NULL
*/
list_for_item_safe(wlist, tlist, ¬ify->list[type]) {
handle = container_of(wlist, struct callback_handle, list);
if ((!receiver || handle->receiver == receiver) &&
(!caller || handle->caller == caller)) {
if (!--handle->num_registrations) {
list_item_del(&handle->list);
rfree(handle);
}
}
}
k_spin_unlock(¬ify->lock, key);
}
void notifier_unregister_all(void *receiver, void *caller)
{
int i;
for (i = NOTIFIER_ID_CPU_FREQ; i < NOTIFIER_ID_COUNT; i++)
notifier_unregister(receiver, caller, i);
}
static void notifier_notify(const void *caller, enum notify_id type, void *data)
{
struct notify *notify = *arch_notify_get();
struct list_item *wlist;
struct list_item *tlist;
struct callback_handle *handle;
/* iterate through notifiers and send event to
* interested clients
*/
list_for_item_safe(wlist, tlist, ¬ify->list[type]) {
handle = container_of(wlist, struct callback_handle, list);
if (!caller || !handle->caller || handle->caller == caller)
handle->cb(handle->receiver, type, data);
}
}
void notifier_notify_remote(void)
{
struct notify *notify = *arch_notify_get();
struct notify_data *notify_data = notify_data_get() + cpu_get_id();
if (!list_is_empty(¬ify->list[notify_data->type])) {
dcache_invalidate_region((__sparse_force void __sparse_cache *)notify_data->data,
notify_data->data_size);
notifier_notify(notify_data->caller, notify_data->type,
notify_data->data);
}
}
void notifier_event(const void *caller, enum notify_id type, uint32_t core_mask,
void *data, uint32_t data_size)
{
struct notify_data *notify_data;
struct idc_msg notify_msg = { IDC_MSG_NOTIFY, IDC_MSG_NOTIFY_EXT };
int i;
/* notify selected targets */
for (i = 0; i < CONFIG_CORE_COUNT; i++) {
if (core_mask & NOTIFIER_TARGET_CORE_MASK(i)) {
if (i == cpu_get_id()) {
notifier_notify(caller, type, data);
} else if (cpu_is_core_enabled(i)) {
notify_msg.core = i;
notify_data = notify_data_get() + i;
notify_data->caller = caller;
notify_data->type = type;
/* NOTE: for transcore events, payload has to
* be allocated on heap, not on stack
*/
notify_data->data = data;
notify_data->data_size = data_size;
dcache_writeback_region((__sparse_force void __sparse_cache *)
notify_data->data, data_size);
idc_send_msg(¬ify_msg, IDC_NON_BLOCKING);
}
}
}
}
void init_system_notify(struct sof *sof)
{
struct notify **notify = arch_notify_get();
int i;
*notify = rzalloc(SOF_MEM_ZONE_SYS, SOF_MEM_FLAG_COHERENT, SOF_MEM_CAPS_RAM,
sizeof(**notify));
k_spinlock_init(&(*notify)->lock);
for (i = NOTIFIER_ID_CPU_FREQ; i < NOTIFIER_ID_COUNT; i++)
list_init(&(*notify)->list[i]);
if (cpu_get_id() == PLATFORM_PRIMARY_CORE_ID)
sof->notify_data = platform_shared_get(notify_data_shared,
sizeof(notify_data_shared));
}
void free_system_notify(void)
{
}