1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-10-30     Bernard      The first version
9  * 2023-09-15     xqyjlj       perf rt_hw_interrupt_disable/enable
10  * 2023-12-10     xqyjlj       spinlock should lock sched
11  * 2024-01-25     Shell        Using rt_exit_critical_safe
12  */
13 #include <rthw.h>
14 #include <rtthread.h>
15 
16 #ifdef RT_USING_SMART
17 #include <lwp.h>
18 #endif
19 
20 #ifdef RT_USING_DEBUG
21 rt_base_t _cpus_critical_level;
22 #endif /* RT_USING_DEBUG */
23 
24 static struct rt_cpu _cpus[RT_CPUS_NR];
25 rt_hw_spinlock_t _cpus_lock;
26 #if defined(RT_DEBUGING_SPINLOCK)
27 void *_cpus_lock_owner = 0;
28 void *_cpus_lock_pc = 0;
29 
30 #endif /* RT_DEBUGING_SPINLOCK */
31 
32 /**
33  * @brief   Initialize a static spinlock object.
34  *
35  * @param   lock is a pointer to the spinlock to initialize.
36  */
rt_spin_lock_init(struct rt_spinlock * lock)37 void rt_spin_lock_init(struct rt_spinlock *lock)
38 {
39     rt_hw_spin_lock_init(&lock->lock);
40 }
RTM_EXPORT(rt_spin_lock_init)41 RTM_EXPORT(rt_spin_lock_init)
42 
43 /**
44  * @brief   This function will lock the spinlock, will lock the thread scheduler.
45  *
46  * @note    If the spinlock is locked, the current CPU will keep polling the spinlock state
47  *          until the spinlock is unlocked.
48  *
49  * @param   lock is a pointer to the spinlock.
50  */
51 void rt_spin_lock(struct rt_spinlock *lock)
52 {
53     rt_enter_critical();
54     rt_hw_spin_lock(&lock->lock);
55     RT_SPIN_LOCK_DEBUG(lock);
56 }
RTM_EXPORT(rt_spin_lock)57 RTM_EXPORT(rt_spin_lock)
58 
59 /**
60  * @brief   This function will unlock the spinlock, will unlock the thread scheduler.
61  *
62  * @note    If the scheduling function is called before unlocking, it will be scheduled in this function.
63  *
64  * @param   lock is a pointer to the spinlock.
65  */
66 void rt_spin_unlock(struct rt_spinlock *lock)
67 {
68     rt_base_t critical_level;
69     RT_SPIN_UNLOCK_DEBUG(lock, critical_level);
70     rt_hw_spin_unlock(&lock->lock);
71     rt_exit_critical_safe(critical_level);
72 }
RTM_EXPORT(rt_spin_unlock)73 RTM_EXPORT(rt_spin_unlock)
74 
75 /**
76  * @brief   This function will disable the local interrupt and then lock the spinlock, will lock the thread scheduler.
77  *
78  * @note    If the spinlock is locked, the current CPU will keep polling the spinlock state
79  *          until the spinlock is unlocked.
80  *
81  * @param   lock is a pointer to the spinlock.
82  *
83  * @return  Return current cpu interrupt status.
84  */
85 rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
86 {
87     rt_base_t level;
88 
89     level = rt_hw_local_irq_disable();
90     rt_enter_critical();
91     rt_hw_spin_lock(&lock->lock);
92     RT_SPIN_LOCK_DEBUG(lock);
93     return level;
94 }
RTM_EXPORT(rt_spin_lock_irqsave)95 RTM_EXPORT(rt_spin_lock_irqsave)
96 
97 /**
98  * @brief   This function will unlock the spinlock and then restore current cpu interrupt status, will unlock the thread scheduler.
99  *
100  * @note    If the scheduling function is called before unlocking, it will be scheduled in this function.
101  *
102  * @param   lock is a pointer to the spinlock.
103  *
104  * @param   level is interrupt status returned by rt_spin_lock_irqsave().
105  */
106 void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
107 {
108     rt_base_t critical_level;
109 
110     RT_SPIN_UNLOCK_DEBUG(lock, critical_level);
111     rt_hw_spin_unlock(&lock->lock);
112     rt_exit_critical_safe(critical_level);
113     rt_hw_local_irq_enable(level);
114 }
RTM_EXPORT(rt_spin_unlock_irqrestore)115 RTM_EXPORT(rt_spin_unlock_irqrestore)
116 
117 /**
118  * @brief   This fucntion will return current cpu object.
119  *
120  * @return  Return a pointer to the current cpu object.
121  */
122 struct rt_cpu *rt_cpu_self(void)
123 {
124     return &_cpus[rt_hw_cpu_id()];
125 }
126 
127 /**
128  * @brief   This fucntion will return the cpu object corresponding to index.
129  *
130  * @param   index is the index of target cpu object.
131  *
132  * @return  Return a pointer to the cpu object corresponding to index.
133  */
rt_cpu_index(int index)134 struct rt_cpu *rt_cpu_index(int index)
135 {
136     return &_cpus[index];
137 }
138 
139 /**
140  * @brief   This function will lock all cpus's scheduler and disable local irq.
141  *
142  * @return  Return current cpu interrupt status.
143  */
rt_cpus_lock(void)144 rt_base_t rt_cpus_lock(void)
145 {
146     rt_base_t level;
147     struct rt_cpu* pcpu;
148 
149     level = rt_hw_local_irq_disable();
150     pcpu = rt_cpu_self();
151     if (pcpu->current_thread != RT_NULL)
152     {
153         rt_ubase_t lock_nest = rt_atomic_load(&(pcpu->current_thread->cpus_lock_nest));
154 
155         rt_atomic_add(&(pcpu->current_thread->cpus_lock_nest), 1);
156         if (lock_nest == 0)
157         {
158             rt_enter_critical();
159             rt_hw_spin_lock(&_cpus_lock);
160 #ifdef RT_USING_DEBUG
161             _cpus_critical_level = rt_critical_level();
162 #endif /* RT_USING_DEBUG */
163 
164 #ifdef RT_DEBUGING_SPINLOCK
165             _cpus_lock_owner = pcpu->current_thread;
166             _cpus_lock_pc = __GET_RETURN_ADDRESS;
167 #endif /* RT_DEBUGING_SPINLOCK */
168         }
169     }
170 
171     return level;
172 }
173 RTM_EXPORT(rt_cpus_lock);
174 
175 /**
176  * @brief   This function will restore all cpus's scheduler and restore local irq.
177  *
178  * @param   level is interrupt status returned by rt_cpus_lock().
179  */
rt_cpus_unlock(rt_base_t level)180 void rt_cpus_unlock(rt_base_t level)
181 {
182     struct rt_cpu* pcpu = rt_cpu_self();
183 
184     if (pcpu->current_thread != RT_NULL)
185     {
186         rt_base_t critical_level = 0;
187         RT_ASSERT(rt_atomic_load(&(pcpu->current_thread->cpus_lock_nest)) > 0);
188         rt_atomic_sub(&(pcpu->current_thread->cpus_lock_nest), 1);
189 
190         if (pcpu->current_thread->cpus_lock_nest == 0)
191         {
192 #if defined(RT_DEBUGING_SPINLOCK)
193             _cpus_lock_owner = __OWNER_MAGIC;
194             _cpus_lock_pc = RT_NULL;
195 #endif /* RT_DEBUGING_SPINLOCK */
196 #ifdef RT_USING_DEBUG
197             critical_level = _cpus_critical_level;
198             _cpus_critical_level = 0;
199 #endif /* RT_USING_DEBUG */
200             rt_hw_spin_unlock(&_cpus_lock);
201             rt_exit_critical_safe(critical_level);
202         }
203     }
204     rt_hw_local_irq_enable(level);
205 }
206 RTM_EXPORT(rt_cpus_unlock);
207 
208 /**
209  * This function is invoked by scheduler.
210  * It will restore the lock state to whatever the thread's counter expects.
211  * If target thread not locked the cpus then unlock the cpus lock.
212  *
213  * @param   thread is a pointer to the target thread.
214  */
rt_cpus_lock_status_restore(struct rt_thread * thread)215 void rt_cpus_lock_status_restore(struct rt_thread *thread)
216 {
217 #if defined(ARCH_MM_MMU) && defined(RT_USING_SMART)
218     lwp_aspace_switch(thread);
219 #endif
220     rt_sched_post_ctx_switch(thread);
221 }
222 RTM_EXPORT(rt_cpus_lock_status_restore);
223 
224 /* A safe API with debugging feature to be called in most codes */
225 
226 #undef rt_cpu_get_id
227 /**
228  * @brief Get logical CPU ID
229  *
230  * @return logical CPU ID
231  */
rt_cpu_get_id(void)232 rt_base_t rt_cpu_get_id(void)
233 {
234 
235     RT_ASSERT(rt_sched_thread_is_binding(RT_NULL) ||
236               rt_hw_interrupt_is_disabled() ||
237               !rt_scheduler_is_available());
238 
239     return rt_hw_cpu_id();
240 }
241