1 /*
2  * Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include "hardware/sync/spin_lock.h"
7 
spin_locks_reset(void)8 void spin_locks_reset(void) {
9     for (uint i = 0; i < NUM_SPIN_LOCKS; i++) {
10         spin_unlock_unsafe(spin_lock_instance(i));
11     }
12 }
13 
spin_lock_init(uint lock_num)14 spin_lock_t *spin_lock_init(uint lock_num) {
15     assert(lock_num < NUM_SPIN_LOCKS);
16     spin_lock_t *lock = spin_lock_instance(lock_num);
17     spin_unlock_unsafe(lock);
18     return lock;
19 }
20 
21 #if PICO_USE_SW_SPIN_LOCKS
22 spin_lock_t _sw_spin_locks[NUM_SPIN_LOCKS];
23 
24 #if __ARM_ARCH_8M_MAIN__ && !PICO_SW_SPIN_LOCKS_NO_EXTEXCLALL
25 #include "pico/runtime_init.h"
26 #include "hardware/structs/m33.h"
27 
spinlock_set_extexclall(void)28 static void spinlock_set_extexclall(void) {
29     // Force use of global exclusive monitor for all exclusive load/stores:
30     // makes multicore exclusives work without adding MPU regions. For
31     // something more exotic, like having multicore exclusives in internal
32     // SRAM and also single-core exclusives in external PSRAM (not covered by
33     // the global monitor on RP2350) you must clear this and add your own
34     // Shareable regions.
35     //
36     // Setting PICO_SW_SPIN_LOCKS_NO_EXTEXCLALL == 1 will disable this code
37     m33_hw->actlr |= M33_ACTLR_EXTEXCLALL_BITS;
38 }
39 
40 // PICO_RUNTIME_INIT_SPIN_LOCKS_RESET is fine as resetting them does not require EXTEXCLALL
41 PICO_RUNTIME_INIT_FUNC_PER_CORE(spinlock_set_extexclall, PICO_RUNTIME_INIT_SPIN_LOCKS_RESET);
42 #endif
43 #endif
44 
45