8#ifndef __SOF_COHERENT_H__
9#define __SOF_COHERENT_H__
20#define __coherent __aligned(PLATFORM_DCACHE_ALIGN)
74BUILD_ASSERT(
sizeof(
struct coherent) <= DCACHE_LINE_SIZE,
"DCACHE_LINE_SIZE too small");
81#ifdef COHERENT_CHECK_ALIAS
82#define ADDR_IS_INCOHERENT(_c) assert(!is_uncached(_c))
83#define ADDR_IS_COHERENT(_c) assert(is_uncached(_c))
85#define ADDR_IS_INCOHERENT(_c)
86#define ADDR_IS_COHERENT(_c)
90#ifdef COHERENT_CHECK_NONSHARED_CORES
92#define CORE_CHECK_STRUCT_FIELD uint32_t __core; bool __is_shared
93#define CORE_CHECK_STRUCT_INIT(_c, is_shared) { (_c)->__core = cpu_get_id(); \
94 (_c)->__is_shared = is_shared; }
95#define CORE_CHECK_STRUCT(_c) { assert(!!(_c)->__is_shared == !!is_uncached(_c)); \
96 assert(cpu_get_id() == (_c)->__core || (_c)->__is_shared); }
98#define CHECK_COHERENT_CORE(_c) assert((_c)->core == cpu_get_id())
102#define CORE_CHECK_STRUCT_FIELD
103#define CORE_CHECK_STRUCT_INIT(_c, is_shared)
104#define CORE_CHECK_STRUCT(_c)
106#define CHECK_COHERENT_CORE(_c)
111#define CHECK_ISR() __ASSERT(!k_is_in_isr(), "Attempt to sleep in ISR!")
112#define CHECK_SLEEP(_c) __ASSERT((_c)->sleep_allowed, \
113 "This context hasn't been initialized for sleeping!")
114#define CHECK_ATOMIC(_c) __ASSERT(!(_c)->sleep_allowed, \
115 "This context has been initialized for sleeping!")
117#define CHECK_ISR() assert(!k_is_in_isr())
118#define CHECK_SLEEP(_c) assert((_c)->sleep_allowed)
119#define CHECK_ATOMIC(_c) assert(!(_c)->sleep_allowed)
137 c->
key = k_spin_lock(&c->
lock);
167 struct coherent *uc = cache_to_uncache(c);
174 k_spin_unlock(&uc->
lock, uc->
key);
186 ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
192 c = (
struct coherent *)((uint8_t *)
object + offset);
206#define coherent_init(type, member) __coherent_init(offsetof(type, member), \
216 c->
key = k_spin_lock(&c->
lock);
219 k_spin_unlock(&c->
lock, c->
key);
222#define coherent_shared(object, member) __coherent_shared(&(object)->member, \
228 struct coherent *c,
const size_t size)
241 k_mutex_lock(&c->mutex, K_FOREVER);
261 struct coherent *uc = cache_to_uncache(c);
268 k_mutex_unlock(&uc->mutex);
272static inline void *__coherent_init_thread(
size_t offset,
const size_t size)
276 ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
282 c = (
struct coherent *)((uint8_t *)
object + offset);
285 k_mutex_init(&c->mutex);
296#define coherent_init_thread(type, member) __coherent_init_thread(offsetof(type, member), \
299static inline void __coherent_shared_thread(
struct coherent *c,
const size_t size)
306 k_mutex_lock(&c->mutex, K_FOREVER);
309 k_mutex_unlock(&c->mutex);
312#define coherent_shared_thread(object, member) __coherent_shared_thread(&(object)->member, \
317#define coherent_free(object, member) \
320 ADDR_IS_COHERENT(object); \
322 dcache_writeback_invalidate_region(uncache_to_cache(object), \
336 c->
key = k_spin_lock(&c->
lock);
345 k_spin_unlock(&c->lock, c->key);
352 ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
358 c = (
struct coherent *)((uint8_t *)
object + offset);
369#define coherent_init(type, member) __coherent_init(offsetof(type, member), \
372#define coherent_free(object, member) rfree(object)
376 c->
key = k_spin_lock(&c->
lock);
378 k_spin_unlock(&c->
lock, c->
key);
381#define coherent_shared(object, member) __coherent_shared(&(object)->member, \
386 struct coherent *c,
const size_t size)
389 k_mutex_lock(&c->mutex, K_FOREVER);
398 k_mutex_unlock(&c->mutex);
401static inline void *__coherent_init_thread(
size_t offset,
const size_t size)
405 ALIGN_UP(size, PLATFORM_DCACHE_ALIGN));
411 c = (
struct coherent *)((uint8_t *)
object + offset);
414 k_mutex_init(&c->mutex);
422#define coherent_init_thread(type, member) __coherent_init_thread(offsetof(type, member), \
425static inline void __coherent_shared_thread(
struct coherent *c,
const size_t size)
427 k_mutex_lock(&c->mutex, K_FOREVER);
429 k_mutex_unlock(&c->mutex);
432#define coherent_shared_thread(object, member) __coherent_shared_thread(&(object)->member, \
439#define coherent_acquire_thread coherent_acquire
440#define coherent_release_thread coherent_release
441#define coherent_init_thread coherent_init
442#define coherent_shared_thread coherent_shared
445#define coherent_free_thread coherent_free
447#define is_coherent_shared(object, member) ((object)->member.shared)
static void dcache_writeback_invalidate_region(void __sparse_cache *addr, size_t size)
Definition cache.h:56
static void dcache_invalidate_region(void __sparse_cache *addr, size_t size)
Definition cache.h:44
#define coherent_release_thread
Definition coherent.h:440
#define CHECK_ISR()
Definition coherent.h:117
static void __coherent_shared(struct coherent *c, const size_t size)
Definition coherent.h:374
#define ADDR_IS_COHERENT(_c)
Definition coherent.h:86
#define CHECK_COHERENT_CORE(_c)
Definition coherent.h:106
static void * __coherent_init(size_t offset, const size_t size)
Definition coherent.h:348
static void coherent_release(struct coherent __sparse_cache *c, const size_t size)
Definition coherent.h:341
#define CHECK_SLEEP(_c)
Definition coherent.h:118
static __must_check struct coherent __sparse_cache * coherent_acquire(struct coherent *c, const size_t size)
Definition coherent.h:332
#define ADDR_IS_INCOHERENT(_c)
Definition coherent.h:85
#define __coherent
Definition coherent.h:20
#define coherent_acquire_thread
Definition coherent.h:439
#define CHECK_ATOMIC(_c)
Definition coherent.h:119
#define STATIC_ASSERT(COND, MESSAGE)
Definition common.h:158
#define ALIGN_UP(size, alignment)
Definition common.h:95
#define __must_check
Definition common.h:15
static int cpu_get_id(void)
Definition cpu.h:98
void * rzalloc(uint32_t flags, size_t bytes)
Similar to rmalloc(), guarantees that returned block is zeroed.
#define SOF_MEM_FLAG_USER
Allocate user memory address.
Definition alloc.h:45
#define SOF_MEM_FLAG_COHERENT
Allocate uncached address.
Definition alloc.h:37
static void list_init(struct list_item *list)
Definition list.h:25
#define list_item(item, type, member)
Definition list.h:80
static void k_spinlock_init(struct k_spinlock *lock)
Definition spinlock.h:13
k_spinlock_key_t key
Definition coherent.h:59
uint16_t core
Definition coherent.h:68
struct k_spinlock lock
Definition coherent.h:58
uint8_t shared
Definition coherent.h:67
uint8_t sleep_allowed
Definition coherent.h:65
struct list_item list
Definition coherent.h:69
#define __sparse_force
Definition compiler_attributes.h:12
#define __sparse_cache
Definition compiler_attributes.h:11