cache.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. */
  10. #ifndef CACHE_H__
  11. #define CACHE_H__
  12. #include "opcode.h"
  13. #ifndef ALWAYS_INLINE
  14. #define ALWAYS_INLINE inline __attribute__((always_inline))
  15. #endif
  16. #define rt_hw_cpu_sync() __asm__ volatile(OPC_SYNC:: \
  17. : "memory")
  18. #define rt_hw_cpu_sync_i() __asm__ volatile(OPC_SYNC_I:: \
  19. : "memory");
  20. /**
  21. * ========================================
  22. * Local cpu cache maintainence operations
  23. * ========================================
  24. */
  25. void rt_hw_cpu_dcache_clean_local(void *addr, int size);
  26. void rt_hw_cpu_dcache_invalidate_local(void *addr, int size);
  27. void rt_hw_cpu_dcache_clean_and_invalidate_local(void *addr, int size);
  28. void rt_hw_cpu_icache_invalidate_local(void *addr, int size);
  29. ALWAYS_INLINE void rt_hw_cpu_dcache_clean_all_local(void)
  30. {
  31. __asm__ volatile(OPC_DCACHE_CALL ::
  32. : "memory");
  33. rt_hw_cpu_sync();
  34. }
  35. ALWAYS_INLINE void rt_hw_cpu_dcache_invalidate_all_local(void)
  36. {
  37. __asm__ volatile(OPC_DCACHE_IALL ::
  38. : "memory");
  39. rt_hw_cpu_sync();
  40. }
  41. ALWAYS_INLINE void rt_hw_cpu_dcache_clean_and_invalidate_all_local(void)
  42. {
  43. __asm__ volatile(OPC_DCACHE_CIALL ::
  44. : "memory");
  45. rt_hw_cpu_sync();
  46. }
  47. ALWAYS_INLINE void rt_hw_cpu_icache_invalidate_all_local(void)
  48. {
  49. __asm__ volatile(OPC_ICACHE_IALL ::
  50. : "memory");
  51. rt_hw_cpu_sync_i();
  52. }
  53. #define rt_hw_icache_invalidate_all rt_hw_cpu_icache_invalidate_all
  54. /**
  55. * ========================================
  56. * Multi-core cache maintainence operations
  57. * ========================================
  58. */
  59. #ifdef RT_USING_SMP
  60. #error "TODO: cache maintainence have not ported to RISC-V SMP yet"
  61. void rt_hw_cpu_dcache_clean(void *addr, int size);
  62. void rt_hw_cpu_dcache_invalidate(void *addr, int size);
  63. void rt_hw_cpu_dcache_clean_and_invalidate(void *addr, int size);
  64. void rt_hw_cpu_dcache_clean_all(void);
  65. void rt_hw_cpu_dcache_invalidate_all(void);
  66. void rt_hw_cpu_dcache_clean_and_invalidate_all(void);
  67. void rt_hw_cpu_icache_invalidate(void *addr, int size);
  68. void rt_hw_cpu_icache_invalidate_all(void);
  69. #else /* !RT_USING_SMP */
  70. #define rt_hw_cpu_dcache_clean rt_hw_cpu_dcache_clean_local
  71. #define rt_hw_cpu_dcache_invalidate rt_hw_cpu_dcache_invalidate_local
  72. #define rt_hw_cpu_dcache_clean_and_invalidate rt_hw_cpu_dcache_clean_and_invalidate_local
  73. #define rt_hw_cpu_dcache_clean_all rt_hw_cpu_dcache_clean_all_local
  74. #define rt_hw_cpu_dcache_invalidate_all rt_hw_cpu_dcache_invalidate_all_local
  75. #define rt_hw_cpu_dcache_clean_and_invalidate_all rt_hw_cpu_dcache_clean_and_invalidate_all_local
  76. #define rt_hw_cpu_icache_invalidate rt_hw_cpu_icache_invalidate_local
  77. #define rt_hw_cpu_icache_invalidate_all rt_hw_cpu_icache_invalidate_all_local
  78. #endif /* RT_USING_SMP */
  79. /**
  80. * @brief Synchronize cache to Point of Coherent
  81. */
  82. void rt_hw_sync_cache_local(void *addr, int size);
  83. #endif /* CACHE_H__ */