diff --git a/zephyr/lib/fast-get.c b/zephyr/lib/fast-get.c index 245e078ac77a..8e576d688dee 100644 --- a/zephyr/lib/fast-get.c +++ b/zephyr/lib/fast-get.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -37,33 +38,27 @@ LOG_MODULE_REGISTER(fast_get, CONFIG_SOF_LOG_LEVEL); static int fast_get_realloc(struct sof_fast_get_data *data) { struct sof_fast_get_entry *entries; - - if (!data->num_entries) { - /* - * Allocate 8 entries for the beginning. Currently we only use - * 2 entries at most, so this should provide a reasonable first - * allocation. - */ - const unsigned int n_entries = 8; - - data->entries = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, - n_entries * sizeof(*entries)); - if (!data->entries) - return -ENOMEM; - data->num_entries = n_entries; - return 0; - } - - entries = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, - 2 * data->num_entries * sizeof(*entries)); + /* + * Allocate 8 entries for the beginning. Currently we only use 2 entries + * at most, so this should provide a reasonable first allocation. + */ + const unsigned int init_n_entries = 8; + unsigned int n_entries = data->num_entries ? data->num_entries * 2 : init_n_entries; + + entries = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, SOF_MEM_FLAG_COHERENT, SOF_MEM_CAPS_RAM, + n_entries * sizeof(*entries)); if (!entries) return -ENOMEM; - memcpy_s(entries, 2 * data->num_entries * sizeof(*entries), data->entries, - data->num_entries * sizeof(*entries)); - rfree(data->entries); + if (data->num_entries) { + memcpy_s(entries, n_entries * sizeof(*entries), data->entries, + data->num_entries * sizeof(*entries)); + rfree(data->entries); + } + data->entries = entries; - data->num_entries = 2 * data->num_entries; + data->num_entries = n_entries; + return 0; } @@ -113,6 +108,11 @@ const void *fast_get(const void *dram_ptr, size_t size) ret = entry->sram_ptr; entry->refcount++; + /* + * The data is constant, so it's safe to use cached access to + * it, but initially we have to invalidate cached + */ + dcache_invalidate_region((__sparse_force void __sparse_cache *)ret, size); goto out; } @@ -159,10 +159,10 @@ void fast_put(const void *sram_ptr) goto out; } entry->refcount--; - if (entry->refcount > 0) - goto out; - rfree(entry->sram_ptr); - memset(entry, 0, sizeof(*entry)); + if (!entry->refcount) { + rfree(entry->sram_ptr); + memset(entry, 0, sizeof(*entry)); + } out: tr_dbg(fast_get, "put %p, DRAM %p size %u refcnt %u", sram_ptr, entry ? entry->dram_ptr : 0, entry ? entry->size : 0, entry ? entry->refcount : 0);