rthw.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-18 Bernard the first version
  9. * 2006-04-25 Bernard add rt_hw_context_switch_interrupt declaration
  10. * 2006-09-24 Bernard add rt_hw_context_switch_to declaration
  11. * 2012-12-29 Bernard add rt_hw_exception_install declaration
  12. * 2017-10-17 Hichard add some macros
  13. * 2018-11-17 Jesven add rt_hw_spinlock_t
  14. * add smp support
  15. * 2019-05-18 Bernard add empty definition for not enable cache case
  16. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  17. * 2023-10-16 Shell Support a new backtrace framework
  18. */
  19. #ifndef __RT_HW_H__
  20. #define __RT_HW_H__
  21. #include <rtdef.h>
  22. #if defined (RT_USING_CACHE) || defined(RT_USING_SMP) || defined(RT_HW_INCLUDE_CPUPORT)
  23. #include <cpuport.h> /* include spinlock, cache ops, etc. */
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /*
  29. * Some macros define
  30. */
  31. #ifndef HWREG64
  32. #define HWREG64(x) (*((volatile rt_uint64_t *)(x)))
  33. #endif
  34. #ifndef HWREG32
  35. #define HWREG32(x) (*((volatile rt_uint32_t *)(x)))
  36. #endif
  37. #ifndef HWREG16
  38. #define HWREG16(x) (*((volatile rt_uint16_t *)(x)))
  39. #endif
  40. #ifndef HWREG8
  41. #define HWREG8(x) (*((volatile rt_uint8_t *)(x)))
  42. #endif
  43. #ifndef RT_CPU_CACHE_LINE_SZ
  44. #define RT_CPU_CACHE_LINE_SZ 32
  45. #endif
  46. enum RT_HW_CACHE_OPS
  47. {
  48. RT_HW_CACHE_FLUSH = 0x01,
  49. RT_HW_CACHE_INVALIDATE = 0x02,
  50. };
  51. /*
  52. * CPU interfaces
  53. */
  54. #ifdef RT_USING_CACHE
  55. #ifdef RT_USING_SMART
  56. #include <cache.h>
  57. #endif
  58. void rt_hw_cpu_icache_enable(void);
  59. void rt_hw_cpu_icache_disable(void);
  60. rt_base_t rt_hw_cpu_icache_status(void);
  61. void rt_hw_cpu_icache_ops(int ops, void* addr, int size);
  62. void rt_hw_cpu_dcache_enable(void);
  63. void rt_hw_cpu_dcache_disable(void);
  64. rt_base_t rt_hw_cpu_dcache_status(void);
  65. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size);
  66. #else
  67. /* define cache ops as empty */
  68. #define rt_hw_cpu_icache_enable(...)
  69. #define rt_hw_cpu_icache_disable(...)
  70. #define rt_hw_cpu_icache_ops(...)
  71. #define rt_hw_cpu_dcache_enable(...)
  72. #define rt_hw_cpu_dcache_disable(...)
  73. #define rt_hw_cpu_dcache_ops(...)
  74. #define rt_hw_cpu_icache_status(...) 0
  75. #define rt_hw_cpu_dcache_status(...) 0
  76. #endif
  77. void rt_hw_cpu_reset(void);
  78. void rt_hw_cpu_shutdown(void);
  79. const char *rt_hw_cpu_arch(void);
  80. rt_uint8_t *rt_hw_stack_init(void *entry,
  81. void *parameter,
  82. rt_uint8_t *stack_addr,
  83. void *exit);
  84. #ifdef RT_USING_HW_STACK_GUARD
  85. void rt_hw_stack_guard_init(rt_thread_t thread);
  86. #endif
  87. /*
  88. * Interrupt handler definition
  89. */
  90. typedef void (*rt_isr_handler_t)(int vector, void *param);
  91. struct rt_irq_desc
  92. {
  93. rt_isr_handler_t handler;
  94. void *param;
  95. #ifdef RT_USING_INTERRUPT_INFO
  96. char name[RT_NAME_MAX];
  97. rt_uint32_t counter;
  98. #ifdef RT_USING_SMP
  99. rt_ubase_t cpu_counter[RT_CPUS_NR];
  100. #endif
  101. #endif
  102. };
  103. /*
  104. * Interrupt interfaces
  105. */
  106. void rt_hw_interrupt_init(void);
  107. void rt_hw_interrupt_mask(int vector);
  108. void rt_hw_interrupt_umask(int vector);
  109. rt_isr_handler_t rt_hw_interrupt_install(int vector,
  110. rt_isr_handler_t handler,
  111. void *param,
  112. const char *name);
  113. void rt_hw_interrupt_uninstall(int vector,
  114. rt_isr_handler_t handler,
  115. void *param);
  116. #ifdef RT_USING_SMP
  117. rt_base_t rt_hw_local_irq_disable(void);
  118. void rt_hw_local_irq_enable(rt_base_t level);
  119. rt_base_t rt_cpus_lock(void);
  120. void rt_cpus_unlock(rt_base_t level);
  121. #define rt_hw_interrupt_disable rt_cpus_lock
  122. #define rt_hw_interrupt_enable rt_cpus_unlock
  123. #else
  124. rt_base_t rt_hw_interrupt_disable(void);
  125. void rt_hw_interrupt_enable(rt_base_t level);
  126. #define rt_hw_local_irq_disable rt_hw_interrupt_disable
  127. #define rt_hw_local_irq_enable rt_hw_interrupt_enable
  128. #endif /*RT_USING_SMP*/
  129. rt_bool_t rt_hw_interrupt_is_disabled(void);
  130. /*
  131. * Context interfaces
  132. */
  133. #ifdef RT_USING_SMP
  134. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  135. void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread);
  136. void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  137. #else
  138. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  139. void rt_hw_context_switch_to(rt_ubase_t to);
  140. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread);
  141. #endif /*RT_USING_SMP*/
  142. /**
  143. * Hardware Layer Backtrace Service
  144. */
  145. struct rt_hw_backtrace_frame {
  146. rt_uintptr_t fp;
  147. rt_uintptr_t pc;
  148. };
  149. rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame);
  150. rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame);
  151. void rt_hw_console_output(const char *str);
  152. void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size);
  153. /*
  154. * Exception interfaces
  155. */
  156. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));
  157. /*
  158. * delay interfaces
  159. */
  160. void rt_hw_us_delay(rt_uint32_t us);
  161. int rt_hw_cpu_id(void);
  162. #if defined(RT_USING_SMP) || defined(RT_USING_AMP)
  163. /**
  164. * ipi function
  165. */
  166. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);
  167. #endif
  168. #ifdef RT_USING_SMP
  169. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
  170. void rt_hw_spin_lock(rt_hw_spinlock_t *lock);
  171. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock);
  172. extern rt_hw_spinlock_t _cpus_lock;
  173. #define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0}
  174. #define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \
  175. (rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)
  176. #define RT_DEFINE_HW_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)
  177. /**
  178. * boot secondary cpu
  179. */
  180. void rt_hw_secondary_cpu_up(void);
  181. /**
  182. * secondary cpu idle function
  183. */
  184. void rt_hw_secondary_cpu_idle_exec(void);
  185. #else /* !RT_USING_SMP */
  186. #define RT_DEFINE_HW_SPINLOCK(x) rt_ubase_t x
  187. #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
  188. #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
  189. #endif /* RT_USING_SMP */
  190. #ifndef RT_USING_CACHE
  191. #define rt_hw_isb()
  192. #define rt_hw_dmb()
  193. #define rt_hw_dsb()
  194. #endif /* RT_USING_CACHE */
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif