cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-01-29 lizhirui first version
  9. * 2021-11-05 JasonHu add c906 cache inst
  10. */
  11. #include <rthw.h>
  12. #include <rtdef.h>
  13. #include <board.h>
  14. #include <riscv.h>
  15. #define L1_CACHE_BYTES (64)
  16. static void dcache_wb_range(unsigned long start, unsigned long end)
  17. {
  18. unsigned long i = start & ~(L1_CACHE_BYTES - 1);
  19. for (; i < end; i += L1_CACHE_BYTES)
  20. {
  21. /* asm volatile("dcache.cva %0\n"::"r"(i):"memory"); */
  22. /*
  23. * compiler always use a5 = i.
  24. * a6 not used, so we use a6 here.
  25. */
  26. asm volatile("mv a6, %0\n"::"r"(i):"memory"); /* a6 = a5(i) */
  27. asm volatile(".long 0x0257800b"); /* dcache.cva a6 */
  28. }
  29. asm volatile(".long 0x01b0000b"); /* sync.is */
  30. }
  31. static void dcache_inv_range(unsigned long start, unsigned long end)
  32. {
  33. unsigned long i = start & ~(L1_CACHE_BYTES - 1);
  34. for (; i < end; i += L1_CACHE_BYTES)
  35. {
  36. /* asm volatile("dcache.iva %0\n"::"r"(i):"memory"); */
  37. asm volatile("mv a6, %0\n"::"r"(i):"memory"); /* a6 = a5(i) */
  38. asm volatile(".long 0x0268000b"); /* dcache.iva a6 */
  39. }
  40. asm volatile(".long 0x01b0000b");
  41. }
  42. static void dcache_wbinv_range(unsigned long start, unsigned long end)
  43. {
  44. unsigned long i = start & ~(L1_CACHE_BYTES - 1);
  45. for (; i < end; i += L1_CACHE_BYTES)
  46. {
  47. /* asm volatile("dcache.civa %0\n"::"r"(i):"memory"); */
  48. asm volatile("mv a6, %0\n"::"r"(i):"memory"); /* a6 = a5(i) */
  49. asm volatile(".long 0x0278000b"); /* dcache.civa a6 */
  50. }
  51. asm volatile(".long 0x01b0000b");
  52. }
  53. static void icache_inv_range(unsigned long start, unsigned long end)
  54. {
  55. unsigned long i = start & ~(L1_CACHE_BYTES - 1);
  56. for (; i < end; i += L1_CACHE_BYTES)
  57. {
  58. /* asm volatile("icache.iva %0\n"::"r"(i):"memory"); */
  59. asm volatile("mv a6, %0\n"::"r"(i):"memory"); /* a6 = a5(i) */
  60. asm volatile(".long 0x0308000b"); /* icache.iva a6 */
  61. }
  62. asm volatile(".long 0x01b0000b");
  63. }
  64. rt_inline rt_uint32_t rt_cpu_icache_line_size(void)
  65. {
  66. return L1_CACHE_BYTES;
  67. }
  68. rt_inline rt_uint32_t rt_cpu_dcache_line_size(void)
  69. {
  70. return L1_CACHE_BYTES;
  71. }
  72. void rt_hw_cpu_icache_invalidate(void *addr,int size)
  73. {
  74. icache_inv_range((unsigned long)addr, (unsigned long)((unsigned char *)addr + size));
  75. }
  76. void rt_hw_cpu_dcache_invalidate(void *addr,int size)
  77. {
  78. dcache_inv_range((unsigned long)addr, (unsigned long)((unsigned char *)addr + size));
  79. }
  80. void rt_hw_cpu_dcache_clean(void *addr,int size)
  81. {
  82. dcache_wb_range((unsigned long)addr, (unsigned long)((unsigned char *)addr + size));
  83. }
  84. void rt_hw_cpu_dcache_clean_flush(void *addr,int size)
  85. {
  86. dcache_wbinv_range((unsigned long)addr, (unsigned long)((unsigned char *)addr + size));
  87. }
  88. void rt_hw_cpu_icache_ops(int ops,void *addr,int size)
  89. {
  90. if(ops == RT_HW_CACHE_INVALIDATE)
  91. {
  92. rt_hw_cpu_icache_invalidate(addr, size);
  93. }
  94. }
  95. void rt_hw_cpu_dcache_ops(int ops,void *addr,int size)
  96. {
  97. if(ops == RT_HW_CACHE_FLUSH)
  98. {
  99. rt_hw_cpu_dcache_clean(addr, size);
  100. }
  101. else
  102. {
  103. rt_hw_cpu_dcache_invalidate(addr, size);
  104. }
  105. }
  106. void rt_hw_cpu_dcache_clean_all(void)
  107. {
  108. /* asm volatile("dcache.call\n":::"memory"); */
  109. asm volatile(".long 0x0010000b\n":::"memory");
  110. }
  111. void rt_hw_cpu_dcache_invalidate_all(void)
  112. {
  113. /* asm volatile("dcache.ciall\n":::"memory"); */
  114. asm volatile(".long 0x0030000b\n":::"memory");
  115. }
  116. void rt_hw_cpu_icache_invalidate_all(void)
  117. {
  118. /* asm volatile("icache.iall\n":::"memory"); */
  119. asm volatile(".long 0x0100000b\n":::"memory");
  120. }
  121. int sys_cacheflush(void *addr, int size, int cache)
  122. {
  123. return 0;
  124. }