cache.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * 2021-11-05 JasonHu The first version
  9. * 2022-11-09 WangXiaoyao Distinguish local and broadcast operations
  10. */
  11. #ifndef CACHE_H__
  12. #define CACHE_H__
  13. #include "opcode.h"
  14. #ifndef ALWAYS_INLINE
  15. #define ALWAYS_INLINE inline __attribute__((always_inline))
  16. #endif
  17. ALWAYS_INLINE void rt_hw_cpu_sync(void)
  18. {
  19. asm volatile(OPC_SYNC::
  20. : "memory");
  21. }
  22. ALWAYS_INLINE void rt_hw_cpu_sync_i(void)
  23. {
  24. asm volatile(OPC_SYNC_I::
  25. : "memory");
  26. }
  27. ALWAYS_INLINE void rt_hw_cpu_sync_s(void)
  28. {
  29. asm volatile(OPC_SYNC_S::
  30. : "memory");
  31. }
  32. ALWAYS_INLINE void rt_hw_cpu_sync_is(void)
  33. {
  34. asm volatile(OPC_SYNC_IS::
  35. : "memory");
  36. }
  37. /**
  38. * ========================================
  39. * Local cpu cache maintainence operations
  40. * ========================================
  41. */
  42. void rt_hw_cpu_dcache_clean_local(void *addr, int size);
  43. void rt_hw_cpu_dcache_invalidate_local(void *addr, int size);
  44. void rt_hw_cpu_dcache_clean_invalidate_local(void *addr, int size);
  45. void rt_hw_cpu_icache_invalidate_local(void *addr, int size);
  46. ALWAYS_INLINE void rt_hw_cpu_dcache_clean_all_local(void)
  47. {
  48. __asm__ volatile(OPC_DCACHE_CALL ::
  49. : "memory");
  50. rt_hw_cpu_sync();
  51. }
  52. ALWAYS_INLINE void rt_hw_cpu_dcache_invalidate_all_local(void)
  53. {
  54. __asm__ volatile(OPC_DCACHE_IALL ::
  55. : "memory");
  56. rt_hw_cpu_sync();
  57. }
  58. ALWAYS_INLINE void rt_hw_cpu_dcache_clean_invalidate_all_local(void)
  59. {
  60. __asm__ volatile(OPC_DCACHE_CIALL ::
  61. : "memory");
  62. rt_hw_cpu_sync();
  63. }
  64. ALWAYS_INLINE void rt_hw_cpu_icache_invalidate_all_local(void)
  65. {
  66. __asm__ volatile(OPC_ICACHE_IALL ::
  67. : "memory");
  68. rt_hw_cpu_sync_i();
  69. }
  70. #define rt_hw_icache_invalidate_all rt_hw_cpu_icache_invalidate_all
  71. /**
  72. * ========================================
  73. * Multi-core cache maintainence operations
  74. * ========================================
  75. */
  76. #ifdef RT_USING_SMP
  77. #error "TODO: cache maintainence have not ported to RISC-V SMP yet"
  78. void rt_hw_cpu_dcache_clean(void *addr, int size);
  79. void rt_hw_cpu_dcache_invalidate(void *addr, int size);
  80. void rt_hw_cpu_dcache_clean_invalidate(void *addr, int size);
  81. void rt_hw_cpu_dcache_clean_all(void);
  82. void rt_hw_cpu_dcache_invalidate_all(void);
  83. void rt_hw_cpu_dcache_clean_invalidate_all(void);
  84. void rt_hw_cpu_icache_invalidate(void *addr, int size);
  85. void rt_hw_cpu_icache_invalidate_all(void);
  86. #else /* !RT_USING_SMP */
  87. #define rt_hw_cpu_dcache_clean rt_hw_cpu_dcache_clean_local
  88. #define rt_hw_cpu_dcache_invalidate rt_hw_cpu_dcache_invalidate_local
  89. #define rt_hw_cpu_dcache_clean_and_invalidate rt_hw_cpu_dcache_clean_invalidate_local
  90. #define rt_hw_cpu_dcache_clean_all rt_hw_cpu_dcache_clean_all_local
  91. #define rt_hw_cpu_dcache_invalidate_all rt_hw_cpu_dcache_invalidate_all_local
  92. #define rt_hw_cpu_dcache_clean_invalidate_all rt_hw_cpu_dcache_clean_invalidate_all_local
  93. #define rt_hw_cpu_icache_invalidate rt_hw_cpu_icache_invalidate_local
  94. #define rt_hw_cpu_icache_invalidate_all rt_hw_cpu_icache_invalidate_all_local
  95. #endif /* RT_USING_SMP */
  96. /**
  97. * @brief Synchronize cache to Point of Unification
  98. */
  99. void rt_hw_sync_cache_local(void *addr, int size);
  100. #endif /* CACHE_H__ */