|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2024 Intel Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// Author: Jyri Sarha <jyri.sarha@linux.intel.com> |
| 6 | + |
| 7 | +#include <stdbool.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <errno.h> |
| 10 | + |
| 11 | +#include <sof/lib/fast-get.h> |
| 12 | +#include <rtos/alloc.h> |
| 13 | +#include <rtos/spinlock.h> |
| 14 | +#include <rtos/symbol.h> |
| 15 | +#include <ipc/topology.h> |
| 16 | + |
| 17 | +struct sof_fast_get_entry { |
| 18 | + const void *dram_ptr; |
| 19 | + void *sram_ptr; |
| 20 | + size_t size; |
| 21 | + unsigned int refcount; |
| 22 | +}; |
| 23 | + |
| 24 | +struct sof_fast_get_data { |
| 25 | + struct k_spinlock lock; |
| 26 | + size_t num_entries; |
| 27 | + struct sof_fast_get_entry *entries; |
| 28 | +}; |
| 29 | + |
| 30 | +static struct sof_fast_get_data fast_get_data = { |
| 31 | + .num_entries = 0, |
| 32 | + .entries = NULL, |
| 33 | +}; |
| 34 | + |
| 35 | +LOG_MODULE_REGISTER(fast_get, CONFIG_SOF_LOG_LEVEL); |
| 36 | + |
| 37 | +static int fast_get_realloc(struct sof_fast_get_data *data) |
| 38 | +{ |
| 39 | + struct sof_fast_get_entry *entries; |
| 40 | + |
| 41 | + if (!data->num_entries) { |
| 42 | + data->entries = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, |
| 43 | + 32 * sizeof(*entries)); |
| 44 | + if (!data->entries) |
| 45 | + return -ENOMEM; |
| 46 | + data->num_entries = 32; |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + entries = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, |
| 51 | + 2 * data->num_entries * sizeof(*entries)); |
| 52 | + if (!entries) |
| 53 | + return -ENOMEM; |
| 54 | + |
| 55 | + memcpy_s(entries, 2 * data->num_entries * sizeof(*entries), data->entries, |
| 56 | + data->num_entries * sizeof(*entries)); |
| 57 | + rfree(data->entries); |
| 58 | + data->entries = entries; |
| 59 | + data->num_entries = 2 * data->num_entries; |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +static struct sof_fast_get_entry *fast_get_find_entry(struct sof_fast_get_data *data, |
| 64 | + const void *dram_ptr) |
| 65 | +{ |
| 66 | + int i; |
| 67 | + |
| 68 | + for (i = 0; i < data->num_entries; i++) { |
| 69 | + if (data->entries[i].dram_ptr == dram_ptr) |
| 70 | + return &data->entries[i]; |
| 71 | + } |
| 72 | + |
| 73 | + for (i = 0; i < data->num_entries; i++) { |
| 74 | + if (data->entries[i].dram_ptr == NULL) |
| 75 | + return &data->entries[i]; |
| 76 | + } |
| 77 | + |
| 78 | + return NULL; |
| 79 | +} |
| 80 | + |
| 81 | +const void *fast_get(const void *dram_ptr, size_t size) |
| 82 | +{ |
| 83 | + struct sof_fast_get_data *data = &fast_get_data; |
| 84 | + struct sof_fast_get_entry *entry; |
| 85 | + k_spinlock_key_t key; |
| 86 | + void *ret; |
| 87 | + |
| 88 | + key = k_spin_lock(&data->lock); |
| 89 | + do { |
| 90 | + entry = fast_get_find_entry(data, dram_ptr); |
| 91 | + if (!entry) { |
| 92 | + if (fast_get_realloc(data)) { |
| 93 | + ret = NULL; |
| 94 | + goto out; |
| 95 | + } |
| 96 | + } |
| 97 | + } while (!entry); |
| 98 | + |
| 99 | + if (entry->sram_ptr) { |
| 100 | + if (entry->size != size || entry->dram_ptr != dram_ptr) { |
| 101 | + tr_err(fast_get, "size %u != %u or ptr %p != %p mismatch", |
| 102 | + entry->size, size, entry->dram_ptr, dram_ptr); |
| 103 | + ret = NULL; |
| 104 | + goto out; |
| 105 | + } |
| 106 | + |
| 107 | + ret = entry->sram_ptr; |
| 108 | + entry->refcount++; |
| 109 | + goto out; |
| 110 | + } |
| 111 | + |
| 112 | + ret = rmalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, size); |
| 113 | + if (!ret) |
| 114 | + goto out; |
| 115 | + entry->size = size; |
| 116 | + entry->sram_ptr = ret; |
| 117 | + memcpy_s(entry->sram_ptr, entry->size, dram_ptr, size); |
| 118 | + entry->dram_ptr = dram_ptr; |
| 119 | + entry->refcount = 1; |
| 120 | +out: |
| 121 | + k_spin_unlock(&data->lock, key); |
| 122 | + tr_dbg(fast_get, "get %p, %p, size %u, refcnt %u", dram_ptr, ret, size, |
| 123 | + entry ? entry->refcount : 0); |
| 124 | + |
| 125 | + return ret; |
| 126 | +} |
| 127 | +EXPORT_SYMBOL(fast_get); |
| 128 | + |
| 129 | +static struct sof_fast_get_entry *fast_put_find_entry(struct sof_fast_get_data *data, |
| 130 | + const void *sram_ptr) |
| 131 | +{ |
| 132 | + int i; |
| 133 | + |
| 134 | + for (i = 0; i < data->num_entries; i++) { |
| 135 | + if (data->entries[i].sram_ptr == sram_ptr) |
| 136 | + return &data->entries[i]; |
| 137 | + } |
| 138 | + |
| 139 | + return NULL; |
| 140 | +} |
| 141 | + |
| 142 | +void fast_put(const void *sram_ptr) |
| 143 | +{ |
| 144 | + struct sof_fast_get_data *data = &fast_get_data; |
| 145 | + struct sof_fast_get_entry *entry; |
| 146 | + k_spinlock_key_t key; |
| 147 | + |
| 148 | + key = k_spin_lock(&fast_get_data.lock); |
| 149 | + entry = fast_put_find_entry(data, sram_ptr); |
| 150 | + if (!entry) { |
| 151 | + tr_err(fast_get, "Put called to unknown address %p", sram_ptr); |
| 152 | + goto out; |
| 153 | + } |
| 154 | + entry->refcount--; |
| 155 | + if (entry->refcount > 0) |
| 156 | + goto out; |
| 157 | + rfree(entry->sram_ptr); |
| 158 | + memset(entry, 0, sizeof(*entry)); |
| 159 | +out: |
| 160 | + tr_dbg(fast_get, "put %p, DRAM %p size %u refcnt %u", sram_ptr, entry ? entry->dram_ptr : 0, |
| 161 | + entry ? entry->size : 0, entry ? entry->refcount : 0); |
| 162 | + k_spin_unlock(&data->lock, key); |
| 163 | +} |
| 164 | +EXPORT_SYMBOL(fast_put); |
0 commit comments