rthw.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. */
  17. #ifndef __RT_HW_H__
  18. #define __RT_HW_H__
  19. #include <rtdef.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /*
  24. * Some macros define
  25. */
  26. #ifndef HWREG64
  27. #define HWREG64(x) (*((volatile rt_uint64_t *)(x)))
  28. #endif
  29. #ifndef HWREG32
  30. #define HWREG32(x) (*((volatile rt_uint32_t *)(x)))
  31. #endif
  32. #ifndef HWREG16
  33. #define HWREG16(x) (*((volatile rt_uint16_t *)(x)))
  34. #endif
  35. #ifndef HWREG8
  36. #define HWREG8(x) (*((volatile rt_uint8_t *)(x)))
  37. #endif
  38. #ifndef RT_CPU_CACHE_LINE_SZ
  39. #define RT_CPU_CACHE_LINE_SZ 32
  40. #endif
  41. enum RT_HW_CACHE_OPS
  42. {
  43. RT_HW_CACHE_FLUSH = 0x01,
  44. RT_HW_CACHE_INVALIDATE = 0x02,
  45. };
  46. /*
  47. * CPU interfaces
  48. */
  49. #ifdef RT_USING_CACHE
  50. #ifdef RT_USING_SMART
  51. #include <cache.h>
  52. #endif
  53. void rt_hw_cpu_icache_enable(void);
  54. void rt_hw_cpu_icache_disable(void);
  55. rt_base_t rt_hw_cpu_icache_status(void);
  56. void rt_hw_cpu_icache_ops(int ops, void* addr, int size);
  57. void rt_hw_cpu_dcache_enable(void);
  58. void rt_hw_cpu_dcache_disable(void);
  59. rt_base_t rt_hw_cpu_dcache_status(void);
  60. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size);
  61. #else
  62. /* define cache ops as empty */
  63. #define rt_hw_cpu_icache_enable(...)
  64. #define rt_hw_cpu_icache_disable(...)
  65. #define rt_hw_cpu_icache_ops(...)
  66. #define rt_hw_cpu_dcache_enable(...)
  67. #define rt_hw_cpu_dcache_disable(...)
  68. #define rt_hw_cpu_dcache_ops(...)
  69. #define rt_hw_cpu_icache_status(...) 0
  70. #define rt_hw_cpu_dcache_status(...) 0
  71. #endif
  72. void rt_hw_cpu_reset(void);
  73. void rt_hw_cpu_shutdown(void);
  74. const char *rt_hw_cpu_arch(void);
  75. rt_uint8_t *rt_hw_stack_init(void *entry,
  76. void *parameter,
  77. rt_uint8_t *stack_addr,
  78. void *exit);
  79. /*
  80. * Interrupt handler definition
  81. */
  82. typedef void (*rt_isr_handler_t)(int vector, void *param);
  83. struct rt_irq_desc
  84. {
  85. rt_isr_handler_t handler;
  86. void *param;
  87. #ifdef RT_USING_INTERRUPT_INFO
  88. char name[RT_NAME_MAX];
  89. rt_uint32_t counter;
  90. #endif
  91. };
  92. /*
  93. * Interrupt interfaces
  94. */
  95. void rt_hw_interrupt_init(void);
  96. void rt_hw_interrupt_mask(int vector);
  97. void rt_hw_interrupt_umask(int vector);
  98. rt_isr_handler_t rt_hw_interrupt_install(int vector,
  99. rt_isr_handler_t handler,
  100. void *param,
  101. const char *name);
  102. #ifdef RT_USING_SMP
  103. rt_base_t rt_hw_local_irq_disable();
  104. void rt_hw_local_irq_enable(rt_base_t level);
  105. #define rt_hw_interrupt_disable rt_cpus_lock
  106. #define rt_hw_interrupt_enable rt_cpus_unlock
  107. #else
  108. rt_base_t rt_hw_interrupt_disable(void);
  109. void rt_hw_interrupt_enable(rt_base_t level);
  110. #endif /*RT_USING_SMP*/
  111. rt_bool_t rt_hw_interrupt_is_disabled(void);
  112. /*
  113. * Context interfaces
  114. */
  115. #ifdef RT_USING_SMP
  116. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  117. void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread);
  118. void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  119. #else
  120. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  121. void rt_hw_context_switch_to(rt_ubase_t to);
  122. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread);
  123. #endif /*RT_USING_SMP*/
  124. void rt_hw_console_output(const char *str);
  125. void rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry);
  126. void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size);
  127. /*
  128. * Exception interfaces
  129. */
  130. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));
  131. /*
  132. * delay interfaces
  133. */
  134. void rt_hw_us_delay(rt_uint32_t us);
  135. int rt_hw_cpu_id(void);
  136. #if defined(RT_USING_SMP) || defined(RT_USING_AMP)
  137. /**
  138. * ipi function
  139. */
  140. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);
  141. #endif
  142. #ifdef RT_USING_SMP
  143. #include <cpuport.h> /* for spinlock from arch */
  144. struct rt_spinlock
  145. {
  146. rt_hw_spinlock_t lock;
  147. };
  148. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
  149. void rt_hw_spin_lock(rt_hw_spinlock_t *lock);
  150. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock);
  151. extern rt_hw_spinlock_t _cpus_lock;
  152. extern rt_hw_spinlock_t _rt_critical_lock;
  153. #define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0}
  154. #define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \
  155. (rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)
  156. #define RT_DEFINE_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)
  157. #define RT_DECLARE_SPINLOCK(x)
  158. /**
  159. * boot secondary cpu
  160. */
  161. void rt_hw_secondary_cpu_up(void);
  162. /**
  163. * secondary cpu idle function
  164. */
  165. void rt_hw_secondary_cpu_idle_exec(void);
  166. #else
  167. #define RT_DEFINE_SPINLOCK(x) rt_ubase_t x
  168. #define RT_DECLARE_SPINLOCK(x)
  169. #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
  170. #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
  171. typedef rt_ubase_t rt_spinlock_t;
  172. struct rt_spinlock
  173. {
  174. rt_spinlock_t lock;
  175. };
  176. #endif
  177. #ifdef RT_USING_CACHE
  178. #include <cpuport.h>
  179. #else
  180. #define rt_hw_isb()
  181. #define rt_hw_dmb()
  182. #define rt_hw_dsb()
  183. #endif
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif