forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_pool.c
More file actions
100 lines (82 loc) · 2.73 KB
/
notification_pool.c
File metadata and controls
100 lines (82 loc) · 2.73 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2025 Intel Corporation. All rights reserved.
//
// Author: Adrian Warecki <adrian.warecki@intel.com>
//
#include <stdint.h>
#include <sof/common.h>
#include <sof/list.h>
#include <sof/ipc/notification_pool.h>
#define NOTIFICATION_POOL_MAX_PAYLOAD_SIZE 40 /* IPC4 Resource Event needs 10dw */
#define NOTIFICATION_POOL_MAX_DEPTH 8 /* Maximum number of notifications
* in the pool
*/
LOG_MODULE_REGISTER(notification_pool, CONFIG_SOF_LOG_LEVEL);
SOF_DEFINE_REG_UUID(notification_pool);
DECLARE_TR_CTX(notif_tr, SOF_UUID(notification_pool_uuid), LOG_LEVEL_INFO);
struct ipc_notif_pool_item {
struct ipc_msg msg;
uint32_t payload[SOF_DIV_ROUND_UP(NOTIFICATION_POOL_MAX_PAYLOAD_SIZE, sizeof(uint32_t))];
};
static struct list_item pool_free_list = LIST_INIT(pool_free_list);
static struct k_spinlock pool_free_list_lock;
static int pool_depth;
static void ipc_notif_free(struct ipc_msg *msg)
{
struct ipc_notif_pool_item *item = container_of(msg, struct ipc_notif_pool_item, msg);
k_spinlock_key_t key;
key = k_spin_lock(&pool_free_list_lock);
list_item_append(&item->msg.list, &pool_free_list);
k_spin_unlock(&pool_free_list_lock, key);
}
static struct ipc_msg *ipc_notif_new(size_t size)
{
struct ipc_notif_pool_item *item;
item = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, sizeof(*item));
if (!item) {
tr_err(¬if_tr, "Unable to allocate memory for notification message");
return NULL;
}
list_init(&item->msg.list);
item->msg.tx_data = item->payload;
item->msg.tx_size = size;
item->msg.callback = ipc_notif_free;
return &item->msg;
}
struct ipc_msg *ipc_notification_pool_get(size_t size)
{
struct ipc_notif_pool_item *item;
k_spinlock_key_t key;
struct ipc_msg *new_msg;
if (size > NOTIFICATION_POOL_MAX_PAYLOAD_SIZE) {
tr_err(¬if_tr, "Requested size %zu exceeds maximum payload size %u",
size, NOTIFICATION_POOL_MAX_PAYLOAD_SIZE);
return NULL;
}
/* check if we have a free message */
key = k_spin_lock(&pool_free_list_lock);
if (list_is_empty(&pool_free_list)) {
/* allocate a new message */
if (pool_depth >= NOTIFICATION_POOL_MAX_DEPTH) {
k_spin_unlock(&pool_free_list_lock, key);
tr_err(¬if_tr, "Pool depth exceeded");
return NULL;
}
++pool_depth;
k_spin_unlock(&pool_free_list_lock, key);
new_msg = ipc_notif_new(size);
if (!new_msg) {
key = k_spin_lock(&pool_free_list_lock);
--pool_depth;
k_spin_unlock(&pool_free_list_lock, key);
}
return new_msg;
}
/* take the first free message */
item = list_first_item(&pool_free_list, struct ipc_notif_pool_item, msg.list);
list_item_del(&item->msg.list);
k_spin_unlock(&pool_free_list_lock, key);
item->msg.tx_size = size;
return &item->msg;
}