1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include "sdkconfig.h"
19 #include "soc/cpu.h"
20 #include "hal/cpu_hal.h"
21 #include "soc/soc_memory_layout.h"
22 #include "soc/compare_set.h"
23 
24 #if __XTENSA__
25 #include "xtensa/xtruntime.h"
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #ifdef CONFIG_SPIRAM_WORKAROUND_NEED_VOLATILE_SPINLOCK
33 #define NEED_VOLATILE_MUX volatile
34 #else
35 #define NEED_VOLATILE_MUX
36 #endif
37 
38 #define SPINLOCK_FREE          0xB33FFFFF
39 #define SPINLOCK_WAIT_FOREVER  (-1)
40 #define SPINLOCK_NO_WAIT        0
41 #define SPINLOCK_INITIALIZER   {.owner = SPINLOCK_FREE,.count = 0}
42 #define CORE_ID_REGVAL_XOR_SWAP (0xCDCD ^ 0xABAB)
43 
44 typedef struct {
45     NEED_VOLATILE_MUX uint32_t owner;
46     NEED_VOLATILE_MUX uint32_t count;
47 }spinlock_t;
48 
49 /**
50  * @brief Initialize a lock to its default state - unlocked
51  * @param lock - spinlock object to initialize
52  */
spinlock_initialize(spinlock_t * lock)53 static inline void __attribute__((always_inline)) spinlock_initialize(spinlock_t *lock)
54 {
55     assert(lock);
56 #if !CONFIG_FREERTOS_UNICORE
57     lock->owner = SPINLOCK_FREE;
58     lock->count = 0;
59 #endif
60 }
61 
62 /**
63  * @brief Top level spinlock acquire function, spins until get the lock
64  * @param lock - target spinlock object
65  * @param timeout - cycles to wait, passing SPINLOCK_WAIT_FOREVER blocs indefinitely
66  */
spinlock_acquire(spinlock_t * lock,int32_t timeout)67 static inline bool __attribute__((always_inline)) spinlock_acquire(spinlock_t *lock, int32_t timeout)
68 {
69 #if !CONFIG_FREERTOS_UNICORE && !BOOTLOADER_BUILD
70     uint32_t result;
71     uint32_t irq_status;
72     uint32_t ccount_start;
73     uint32_t core_id, other_core_id;
74 
75     assert(lock);
76     irq_status = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL);
77 
78     if(timeout != SPINLOCK_WAIT_FOREVER){
79         RSR(CCOUNT, ccount_start);
80     }
81 
82     /*spin until we own a core */
83     RSR(PRID, core_id);
84 
85     /* Note: coreID is the full 32 bit core ID (CORE_ID_REGVAL_PRO/CORE_ID_REGVAL_APP) */
86 
87     other_core_id = CORE_ID_REGVAL_XOR_SWAP ^ core_id;
88     do {
89 
90         /* lock->owner should be one of SPINLOCK_FREE, CORE_ID_REGVAL_PRO,
91          * CORE_ID_REGVAL_APP:
92          *  - If SPINLOCK_FREE, we want to atomically set to 'core_id'.
93          *  - If "our" core_id, we can drop through immediately.
94          *  - If "other_core_id", we spin here.
95          */
96         result = core_id;
97 
98 #if defined(CONFIG_ESP32_SPIRAM_SUPPORT)
99         if (esp_ptr_external_ram(lock)) {
100             compare_and_set_extram(&lock->owner, SPINLOCK_FREE, &result);
101         } else {
102 #endif
103         compare_and_set_native(&lock->owner, SPINLOCK_FREE, &result);
104 #if defined(CONFIG_ESP32_SPIRAM_SUPPORT)
105         }
106 #endif
107         if(result != other_core_id) {
108             break;
109         }
110 
111         if (timeout != SPINLOCK_WAIT_FOREVER) {
112             uint32_t ccount_now;
113             ccount_now = cpu_hal_get_cycle_count();
114             if (ccount_now - ccount_start > (unsigned)timeout) {
115                 XTOS_RESTORE_INTLEVEL(irq_status);
116                 return false;
117             }
118         }
119     }while(1);
120 
121     /* any other value implies memory corruption or uninitialized mux */
122     assert(result == core_id || result == SPINLOCK_FREE);
123     assert((result == SPINLOCK_FREE) == (lock->count == 0)); /* we're first to lock iff count is zero */
124     assert(lock->count < 0xFF); /* Bad count value implies memory corruption */
125 
126     lock->count++;
127     XTOS_RESTORE_INTLEVEL(irq_status);
128     return true;
129 
130 #else  // !CONFIG_FREERTOS_UNICORE
131     return true;
132 #endif
133 }
134 
135 /**
136  * @brief Top level spinlock unlock function, unlocks a previously locked spinlock
137  * @param lock - target, locked before, spinlock object
138  */
spinlock_release(spinlock_t * lock)139 static inline void __attribute__((always_inline)) spinlock_release(spinlock_t *lock)
140 {
141 #if !CONFIG_FREERTOS_UNICORE && !BOOTLOADER_BUILD
142     uint32_t irq_status;
143     uint32_t core_id;
144 
145     assert(lock);
146     irq_status = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL);
147 
148     RSR(PRID, core_id);
149     assert(core_id == lock->owner); // This is a mutex we didn't lock, or it's corrupt
150     lock->count--;
151 
152     if(!lock->count) {
153         lock->owner = SPINLOCK_FREE;
154     } else {
155         assert(lock->count < 0x100); // Indicates memory corruption
156     }
157 
158     XTOS_RESTORE_INTLEVEL(irq_status);
159 #endif
160 }
161 
162 #ifdef __cplusplus
163 }
164 #endif
165