rthw.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <rtthread.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. void rt_hw_cpu_icache_enable(void);
  51. void rt_hw_cpu_icache_disable(void);
  52. rt_base_t rt_hw_cpu_icache_status(void);
  53. void rt_hw_cpu_icache_ops(int ops, void* addr, int size);
  54. void rt_hw_cpu_dcache_enable(void);
  55. void rt_hw_cpu_dcache_disable(void);
  56. rt_base_t rt_hw_cpu_dcache_status(void);
  57. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size);
  58. #else
  59. /* define cache ops as empty */
  60. #define rt_hw_cpu_icache_enable(...)
  61. #define rt_hw_cpu_icache_disable(...)
  62. #define rt_hw_cpu_icache_ops(...)
  63. #define rt_hw_cpu_dcache_enable(...)
  64. #define rt_hw_cpu_dcache_disable(...)
  65. #define rt_hw_cpu_dcache_ops(...)
  66. #define rt_hw_cpu_icache_status(...) 0
  67. #define rt_hw_cpu_dcache_status(...) 0
  68. #endif
  69. void rt_hw_cpu_reset(void);
  70. void rt_hw_cpu_shutdown(void);
  71. rt_uint8_t *rt_hw_stack_init(void *entry,
  72. void *parameter,
  73. rt_uint8_t *stack_addr,
  74. void *exit);
  75. /*
  76. * Interrupt handler definition
  77. */
  78. typedef void (*rt_isr_handler_t)(int vector, void *param);
  79. struct rt_irq_desc
  80. {
  81. rt_isr_handler_t handler;
  82. void *param;
  83. #ifdef RT_USING_INTERRUPT_INFO
  84. char name[RT_NAME_MAX];
  85. rt_uint32_t counter;
  86. #endif
  87. };
  88. /*
  89. * Interrupt interfaces
  90. */
  91. void rt_hw_interrupt_init(void);
  92. void rt_hw_interrupt_mask(int vector);
  93. void rt_hw_interrupt_umask(int vector);
  94. rt_isr_handler_t rt_hw_interrupt_install(int vector,
  95. rt_isr_handler_t handler,
  96. void *param,
  97. const char *name);
  98. #ifdef RT_USING_SMP
  99. rt_base_t rt_hw_local_irq_disable();
  100. void rt_hw_local_irq_enable(rt_base_t level);
  101. #define rt_hw_interrupt_disable rt_cpus_lock
  102. #define rt_hw_interrupt_enable rt_cpus_unlock
  103. #else
  104. rt_base_t rt_hw_interrupt_disable(void);
  105. void rt_hw_interrupt_enable(rt_base_t level);
  106. #endif /*RT_USING_SMP*/
  107. /*
  108. * Context interfaces
  109. */
  110. #ifdef RT_USING_SMP
  111. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  112. void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread);
  113. void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  114. #else
  115. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  116. void rt_hw_context_switch_to(rt_ubase_t to);
  117. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread);
  118. #endif /*RT_USING_SMP*/
  119. void rt_hw_console_output(const char *str);
  120. void rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry);
  121. void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size);
  122. /*
  123. * Exception interfaces
  124. */
  125. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));
  126. /*
  127. * delay interfaces
  128. */
  129. void rt_hw_us_delay(rt_uint32_t us);
  130. #ifdef RT_USING_SMP
  131. #include <cpuport.h> /* for spinlock from arch */
  132. struct rt_spinlock
  133. {
  134. rt_hw_spinlock_t lock;
  135. };
  136. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
  137. void rt_hw_spin_lock(rt_hw_spinlock_t *lock);
  138. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock);
  139. int rt_hw_cpu_id(void);
  140. extern rt_hw_spinlock_t _cpus_lock;
  141. extern rt_hw_spinlock_t _rt_critical_lock;
  142. #define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0}
  143. #define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \
  144. (rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)
  145. #define RT_DEFINE_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)
  146. #define RT_DECLARE_SPINLOCK(x)
  147. /**
  148. * ipi function
  149. */
  150. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);
  151. /**
  152. * boot secondary cpu
  153. */
  154. void rt_hw_secondary_cpu_up(void);
  155. /**
  156. * secondary cpu idle function
  157. */
  158. void rt_hw_secondary_cpu_idle_exec(void);
  159. #else
  160. #define RT_DEFINE_SPINLOCK(x) rt_ubase_t x
  161. #define RT_DECLARE_SPINLOCK(x)
  162. #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
  163. #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
  164. typedef int rt_spinlock_t;
  165. #endif
  166. #ifdef RT_USING_CACHE
  167. #include <cpuport.h>
  168. #else
  169. #define rt_hw_isb()
  170. #define rt_hw_dmb()
  171. #define rt_hw_dsb()
  172. #endif
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif