cpu.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #ifdef RT_USING_SMART
  13. #include <lwp.h>
  14. #endif
  15. #ifdef RT_USING_SMP
  16. static struct rt_cpu _cpus[RT_CPUS_NR];
  17. rt_hw_spinlock_t _cpus_lock;
  18. /*
  19. * @brief disable scheduler
  20. */
  21. static void _cpu_preempt_disable(void)
  22. {
  23. rt_base_t level;
  24. struct rt_thread *current_thread;
  25. /* disable interrupt */
  26. level = rt_hw_local_irq_disable();
  27. current_thread = rt_thread_self();
  28. if (!current_thread)
  29. {
  30. rt_hw_local_irq_enable(level);
  31. return;
  32. }
  33. /* lock scheduler for local cpu */
  34. current_thread->scheduler_lock_nest ++;
  35. /* enable interrupt */
  36. rt_hw_local_irq_enable(level);
  37. }
  38. /*
  39. * @brief enable scheduler
  40. */
  41. static void _cpu_preempt_enable(void)
  42. {
  43. rt_base_t level;
  44. struct rt_thread *current_thread;
  45. /* disable interrupt */
  46. level = rt_hw_local_irq_disable();
  47. current_thread = rt_thread_self();
  48. if (!current_thread)
  49. {
  50. rt_hw_local_irq_enable(level);
  51. return;
  52. }
  53. /* unlock scheduler for local cpu */
  54. current_thread->scheduler_lock_nest --;
  55. rt_schedule();
  56. /* enable interrupt */
  57. rt_hw_local_irq_enable(level);
  58. }
  59. /**
  60. * @brief Initialize a static spinlock object.
  61. *
  62. * @param lock is a pointer to the spinlock to initialize.
  63. */
  64. void rt_spin_lock_init(struct rt_spinlock *lock)
  65. {
  66. rt_hw_spin_lock_init(&lock->lock);
  67. }
  68. RTM_EXPORT(rt_spin_lock_init)
  69. /**
  70. * @brief This function will lock the spinlock.
  71. *
  72. * @note If the spinlock is locked, the current CPU will keep polling the spinlock state
  73. * until the spinlock is unlocked.
  74. *
  75. * @param lock is a pointer to the spinlock.
  76. */
  77. void rt_spin_lock(struct rt_spinlock *lock)
  78. {
  79. _cpu_preempt_disable();
  80. rt_hw_spin_lock(&lock->lock);
  81. }
  82. RTM_EXPORT(rt_spin_lock)
  83. /**
  84. * @brief This function will unlock the spinlock.
  85. *
  86. * @param lock is a pointer to the spinlock.
  87. */
  88. void rt_spin_unlock(struct rt_spinlock *lock)
  89. {
  90. rt_hw_spin_unlock(&lock->lock);
  91. _cpu_preempt_enable();
  92. }
  93. RTM_EXPORT(rt_spin_unlock)
  94. /**
  95. * @brief This function will disable the local interrupt and then lock the spinlock.
  96. *
  97. * @note If the spinlock is locked, the current CPU will keep polling the spinlock state
  98. * until the spinlock is unlocked.
  99. *
  100. * @param lock is a pointer to the spinlock.
  101. *
  102. * @return Return current cpu interrupt status.
  103. */
  104. rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
  105. {
  106. unsigned long level;
  107. _cpu_preempt_disable();
  108. level = rt_hw_local_irq_disable();
  109. rt_hw_spin_lock(&lock->lock);
  110. return level;
  111. }
  112. RTM_EXPORT(rt_spin_lock_irqsave)
  113. /**
  114. * @brief This function will unlock the spinlock and then restore current cpu interrupt status.
  115. *
  116. * @param lock is a pointer to the spinlock.
  117. *
  118. * @param level is interrupt status returned by rt_spin_lock_irqsave().
  119. */
  120. void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
  121. {
  122. rt_hw_spin_unlock(&lock->lock);
  123. rt_hw_local_irq_enable(level);
  124. _cpu_preempt_enable();
  125. }
  126. RTM_EXPORT(rt_spin_unlock_irqrestore)
  127. /**
  128. * @brief This fucntion will return current cpu object.
  129. *
  130. * @return Return a pointer to the current cpu object.
  131. */
  132. struct rt_cpu *rt_cpu_self(void)
  133. {
  134. return &_cpus[rt_hw_cpu_id()];
  135. }
  136. /**
  137. * @brief This fucntion will return the cpu object corresponding to index.
  138. *
  139. * @param index is the index of target cpu object.
  140. *
  141. * @return Return a pointer to the cpu object corresponding to index.
  142. */
  143. struct rt_cpu *rt_cpu_index(int index)
  144. {
  145. return &_cpus[index];
  146. }
  147. /**
  148. * @brief This function will lock all cpus's scheduler and disable local irq.
  149. *
  150. * @return Return current cpu interrupt status.
  151. */
  152. rt_base_t rt_cpus_lock(void)
  153. {
  154. rt_base_t level;
  155. struct rt_cpu* pcpu;
  156. level = rt_hw_local_irq_disable();
  157. pcpu = rt_cpu_self();
  158. if (pcpu->current_thread != RT_NULL)
  159. {
  160. register rt_ubase_t lock_nest = pcpu->current_thread->cpus_lock_nest;
  161. pcpu->current_thread->cpus_lock_nest++;
  162. if (lock_nest == 0)
  163. {
  164. pcpu->current_thread->scheduler_lock_nest++;
  165. rt_hw_spin_lock(&_cpus_lock);
  166. }
  167. }
  168. return level;
  169. }
  170. RTM_EXPORT(rt_cpus_lock);
  171. /**
  172. * @brief This function will restore all cpus's scheduler and restore local irq.
  173. *
  174. * @param level is interrupt status returned by rt_cpus_lock().
  175. */
  176. void rt_cpus_unlock(rt_base_t level)
  177. {
  178. struct rt_cpu* pcpu = rt_cpu_self();
  179. if (pcpu->current_thread != RT_NULL)
  180. {
  181. RT_ASSERT(pcpu->current_thread->cpus_lock_nest > 0);
  182. pcpu->current_thread->cpus_lock_nest--;
  183. if (pcpu->current_thread->cpus_lock_nest == 0)
  184. {
  185. pcpu->current_thread->scheduler_lock_nest--;
  186. rt_hw_spin_unlock(&_cpus_lock);
  187. }
  188. }
  189. rt_hw_local_irq_enable(level);
  190. }
  191. RTM_EXPORT(rt_cpus_unlock);
  192. /**
  193. * This function is invoked by scheduler.
  194. * It will restore the lock state to whatever the thread's counter expects.
  195. * If target thread not locked the cpus then unlock the cpus lock.
  196. *
  197. * @param thread is a pointer to the target thread.
  198. */
  199. void rt_cpus_lock_status_restore(struct rt_thread *thread)
  200. {
  201. struct rt_cpu* pcpu = rt_cpu_self();
  202. #if defined(ARCH_MM_MMU) && defined(RT_USING_SMART)
  203. lwp_aspace_switch(thread);
  204. #endif
  205. pcpu->current_thread = thread;
  206. if (!thread->cpus_lock_nest)
  207. {
  208. rt_hw_spin_unlock(&_cpus_lock);
  209. }
  210. }
  211. RTM_EXPORT(rt_cpus_lock_status_restore);
  212. #endif