rthw.h 5.4 KB

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