1 /*
2  * Copyright (c) 2018,2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <kernel_internal.h>
7 #include <zephyr/spinlock.h>
8 #include <zephyr/llext/symbol.h>
9 
z_spin_lock_valid(struct k_spinlock * l)10 bool z_spin_lock_valid(struct k_spinlock *l)
11 {
12 	uintptr_t thread_cpu = l->thread_cpu;
13 
14 	if (thread_cpu != 0U) {
15 		if ((thread_cpu & 3U) == _current_cpu->id) {
16 			return false;
17 		}
18 	}
19 	return true;
20 }
21 EXPORT_SYMBOL(z_spin_lock_valid);
22 
z_spin_unlock_valid(struct k_spinlock * l)23 bool z_spin_unlock_valid(struct k_spinlock *l)
24 {
25 	uintptr_t tcpu = l->thread_cpu;
26 
27 	l->thread_cpu = 0;
28 
29 	if (arch_is_in_isr() && _current->base.thread_state & _THREAD_DUMMY) {
30 		/* Edge case where an ISR aborted _current */
31 		return true;
32 	}
33 	if (tcpu != (_current_cpu->id | (uintptr_t)_current)) {
34 		return false;
35 	}
36 	return true;
37 }
38 EXPORT_SYMBOL(z_spin_unlock_valid);
39 
z_spin_lock_set_owner(struct k_spinlock * l)40 void z_spin_lock_set_owner(struct k_spinlock *l)
41 {
42 	l->thread_cpu = _current_cpu->id | (uintptr_t)_current;
43 }
44 EXPORT_SYMBOL(z_spin_lock_set_owner);
45 
46 #ifdef CONFIG_KERNEL_COHERENCE
z_spin_lock_mem_coherent(struct k_spinlock * l)47 bool z_spin_lock_mem_coherent(struct k_spinlock *l)
48 {
49 	return arch_mem_coherent((void *)l);
50 }
51 EXPORT_SYMBOL(z_spin_lock_mem_coherent);
52 #endif /* CONFIG_KERNEL_COHERENCE */
53