|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2025 Intel Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// Author: Adrian Warecki <adrian.warecki@intel.com> |
| 6 | +// |
| 7 | + |
| 8 | +#include <stdint.h> |
| 9 | +#include <sof/common.h> |
| 10 | +#include <sof/list.h> |
| 11 | +#include <sof/ipc/notification_pool.h> |
| 12 | + |
| 13 | +#define NOTIFICATION_POOL_MAX_PAYLOAD_SIZE 40 /* IPC4 Resource Event needs 10dw */ |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(notification_pool, CONFIG_SOF_LOG_LEVEL); |
| 16 | + |
| 17 | +SOF_DEFINE_REG_UUID(notification_pool); |
| 18 | + |
| 19 | +DECLARE_TR_CTX(notif_tr, SOF_UUID(notification_pool_uuid), LOG_LEVEL_INFO); |
| 20 | + |
| 21 | +struct ipc_notif_pool_item { |
| 22 | + struct ipc_msg msg; |
| 23 | + uint32_t payload[SOF_DIV_ROUND_UP(NOTIFICATION_POOL_MAX_PAYLOAD_SIZE, sizeof(uint32_t))]; |
| 24 | +}; |
| 25 | + |
| 26 | +static struct list_item pool_free_list = LIST_INIT(pool_free_list); |
| 27 | +struct k_spinlock pool_free_list_lock; |
| 28 | + |
| 29 | +static void ipc_notif_free(struct ipc_msg *msg) |
| 30 | +{ |
| 31 | + struct ipc_notif_pool_item *item = container_of(msg, struct ipc_notif_pool_item, msg); |
| 32 | + k_spinlock_key_t key; |
| 33 | + |
| 34 | + key = k_spin_lock(&pool_free_list_lock); |
| 35 | + list_item_append(&item->msg.list, &pool_free_list); |
| 36 | + k_spin_unlock(&pool_free_list_lock, key); |
| 37 | +} |
| 38 | + |
| 39 | +static struct ipc_msg *ipc_notif_new(size_t size) |
| 40 | +{ |
| 41 | + struct ipc_notif_pool_item *item; |
| 42 | + item = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, sizeof(*item)); |
| 43 | + if (!item) { |
| 44 | + tr_err(¬if_tr, "Unable to allocate memory for notification message"); |
| 45 | + return NULL; |
| 46 | + } |
| 47 | + |
| 48 | + list_init(&item->msg.list); |
| 49 | + item->msg.tx_data = item->payload; |
| 50 | + item->msg.tx_size = size; |
| 51 | + item->msg.callback = ipc_notif_free; |
| 52 | + return &item->msg; |
| 53 | +} |
| 54 | + |
| 55 | +struct ipc_msg *ipc_notification_pool_get(size_t size) |
| 56 | +{ |
| 57 | + struct ipc_notif_pool_item *item; |
| 58 | + k_spinlock_key_t key; |
| 59 | + |
| 60 | + if (size > NOTIFICATION_POOL_MAX_PAYLOAD_SIZE) { |
| 61 | + tr_err(¬if_tr, "Requested size %u exceeds maximum payload size %u", |
| 62 | + size, NOTIFICATION_POOL_MAX_PAYLOAD_SIZE); |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + |
| 66 | + /* check if we have a free message */ |
| 67 | + key = k_spin_lock(&pool_free_list_lock); |
| 68 | + if (list_is_empty(&pool_free_list)) { |
| 69 | + k_spin_unlock(&pool_free_list_lock, key); |
| 70 | + return ipc_notif_new(size); |
| 71 | + } |
| 72 | + |
| 73 | + /* take the first free message */ |
| 74 | + item = list_first_item(&pool_free_list, struct ipc_notif_pool_item, msg.list); |
| 75 | + list_item_del(&item->msg.list); |
| 76 | + k_spin_unlock(&pool_free_list_lock, key); |
| 77 | + |
| 78 | + item->msg.tx_size = size; |
| 79 | + return &item->msg; |
| 80 | +} |
0 commit comments