rthw.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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)
  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. /*
  85. * Interrupt handler definition
  86. */
  87. typedef void (*rt_isr_handler_t)(int vector, void *param);
  88. struct rt_irq_desc
  89. {
  90. rt_isr_handler_t handler;
  91. void *param;
  92. #ifdef RT_USING_INTERRUPT_INFO
  93. char name[RT_NAME_MAX];
  94. rt_uint32_t counter;
  95. #ifdef RT_USING_SMP
  96. rt_ubase_t cpu_counter[RT_CPUS_NR];
  97. #endif
  98. #endif
  99. };
  100. /*
  101. * Interrupt interfaces
  102. */
  103. void rt_hw_interrupt_init(void);
  104. void rt_hw_interrupt_mask(int vector);
  105. void rt_hw_interrupt_umask(int vector);
  106. rt_isr_handler_t rt_hw_interrupt_install(int vector,
  107. rt_isr_handler_t handler,
  108. void *param,
  109. const char *name);
  110. #ifdef RT_USING_SMP
  111. rt_base_t rt_hw_local_irq_disable();
  112. void rt_hw_local_irq_enable(rt_base_t level);
  113. rt_base_t rt_cpus_lock(void);
  114. void rt_cpus_unlock(rt_base_t level);
  115. #define rt_hw_interrupt_disable rt_cpus_lock
  116. #define rt_hw_interrupt_enable rt_cpus_unlock
  117. #else
  118. rt_base_t rt_hw_interrupt_disable(void);
  119. void rt_hw_interrupt_enable(rt_base_t level);
  120. #define rt_hw_local_irq_disable rt_hw_interrupt_disable
  121. #define rt_hw_local_irq_enable rt_hw_interrupt_enable
  122. #endif /*RT_USING_SMP*/
  123. rt_bool_t rt_hw_interrupt_is_disabled(void);
  124. /*
  125. * Context interfaces
  126. */
  127. #ifdef RT_USING_SMP
  128. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  129. void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread);
  130. void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  131. #else
  132. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  133. void rt_hw_context_switch_to(rt_ubase_t to);
  134. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread);
  135. #endif /*RT_USING_SMP*/
  136. /**
  137. * Hardware Layer Backtrace Service
  138. */
  139. struct rt_hw_backtrace_frame {
  140. rt_base_t fp;
  141. rt_base_t pc;
  142. };
  143. rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame);
  144. rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame);
  145. void rt_hw_console_output(const char *str);
  146. void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size);
  147. /*
  148. * Exception interfaces
  149. */
  150. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));
  151. /*
  152. * delay interfaces
  153. */
  154. void rt_hw_us_delay(rt_uint32_t us);
  155. int rt_hw_cpu_id(void);
  156. #if defined(RT_USING_SMP) || defined(RT_USING_AMP)
  157. /**
  158. * ipi function
  159. */
  160. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);
  161. #endif
  162. #ifdef RT_USING_SMP
  163. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
  164. void rt_hw_spin_lock(rt_hw_spinlock_t *lock);
  165. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock);
  166. extern rt_hw_spinlock_t _cpus_lock;
  167. #define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0}
  168. #define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \
  169. (rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)
  170. #define RT_DEFINE_HW_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)
  171. /**
  172. * boot secondary cpu
  173. */
  174. void rt_hw_secondary_cpu_up(void);
  175. /**
  176. * secondary cpu idle function
  177. */
  178. void rt_hw_secondary_cpu_idle_exec(void);
  179. #else
  180. #define RT_DEFINE_HW_SPINLOCK(x) rt_ubase_t x
  181. #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
  182. #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
  183. #endif
  184. #ifndef RT_USING_CACHE
  185. #define rt_hw_isb()
  186. #define rt_hw_dmb()
  187. #define rt_hw_dsb()
  188. #endif
  189. #ifdef __cplusplus
  190. }
  191. #endif
  192. #endif