cache.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "nds32.h"
  2. #include "cache.h"
  3. #include "string.h"
  4. void nds32_dcache_invalidate(void){
  5. #ifdef CONFIG_CPU_DCACHE_ENABLE
  6. __nds32__cctl_l1d_invalall();
  7. __nds32__msync_store();
  8. __nds32__dsb();
  9. #endif
  10. }
  11. void nds32_dcache_flush(void){
  12. #ifdef CONFIG_CPU_DCACHE_ENABLE
  13. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  14. unsigned long saved_gie;
  15. #endif
  16. unsigned long end;
  17. unsigned long cache_line;
  18. cache_line = CACHE_LINE_SIZE(DCACHE);
  19. end = CACHE_WAY(DCACHE) * CACHE_SET(DCACHE) * cache_line;
  20. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  21. GIE_SAVE(&saved_gie);
  22. /*
  23. * Use CCTL L1D_IX_WB/L1D_IX_INVAL subtype instead of combined
  24. * L1D_IX_WBINVAL. Because only N903 supports L1D_IX_WBINVAL.
  25. */
  26. do {
  27. end -= cache_line;
  28. __nds32__cctlidx_wbinval(NDS32_CCTL_L1D_IX_WB, end);
  29. __nds32__cctlidx_wbinval(NDS32_CCTL_L1D_IX_INVAL, end);
  30. } while (end > 0);
  31. GIE_RESTORE(saved_gie);
  32. #else
  33. while (end > 0){
  34. end -= cache_line;
  35. __nds32__cctlidx_wbinval(NDS32_CCTL_L1D_IX_INVAL, end);
  36. }
  37. #endif
  38. __nds32__msync_store();
  39. __nds32__dsb();
  40. #endif
  41. }
  42. void nds32_icache_flush(void){
  43. #ifdef CONFIG_CPU_ICACHE_ENABLE
  44. unsigned long end;
  45. unsigned long cache_line = CACHE_LINE_SIZE(ICACHE);
  46. end = CACHE_WAY(ICACHE) * CACHE_SET(ICACHE) * CACHE_LINE_SIZE(ICACHE);
  47. do {
  48. end -= cache_line;
  49. __nds32__cctlidx_wbinval(NDS32_CCTL_L1I_IX_INVAL, end);
  50. } while (end > 0);
  51. __nds32__isb();
  52. #endif
  53. }
  54. #ifdef CONFIG_CHECK_RANGE_ALIGNMENT
  55. #define chk_range_alignment(start, end, line_size) do { \
  56. \
  57. BUG_ON((start) & ((line_size) - 1)); \
  58. BUG_ON((end) & ((line_size) - 1)); \
  59. BUG_ON((start) == (end)); \
  60. \
  61. } while (0);
  62. #else
  63. #define chk_range_alignment(start, end, line_size)
  64. #endif
  65. /* ================================ D-CACHE =============================== */
  66. /*
  67. * nds32_dcache_clean_range(start, end)
  68. *
  69. * For the specified virtual address range, ensure that all caches contain
  70. * clean data, such that peripheral accesses to the physical RAM fetch
  71. * correct data.
  72. */
  73. void nds32_dcache_clean_range(unsigned long start, unsigned long end){
  74. #ifdef CONFIG_CPU_DCACHE_ENABLE
  75. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  76. unsigned long line_size;
  77. line_size = CACHE_LINE_SIZE(DCACHE);
  78. chk_range_alignment(start, end, line_size);
  79. while (end > start){
  80. __nds32__cctlva_wbinval_one_lvl(NDS32_CCTL_L1D_VA_WB, (void *)start);
  81. start += line_size;
  82. }
  83. __nds32__msync_store();
  84. __nds32__dsb();
  85. #endif
  86. #endif
  87. }
  88. void nds32_dma_clean_range(unsigned long start, unsigned long end){
  89. unsigned long line_size;
  90. line_size = CACHE_LINE_SIZE(DCACHE);
  91. start = start & (~(line_size-1));
  92. end = (end + line_size -1) & (~(line_size-1));
  93. if (start == end)
  94. return;
  95. nds32_dcache_clean_range(start, end);
  96. }
  97. /*
  98. * nds32_dcache_invalidate_range(start, end)
  99. *
  100. * throw away all D-cached data in specified region without an obligation
  101. * to write them back. Note however that we must clean the D-cached entries
  102. * around the boundaries if the start and/or end address are not cache
  103. * aligned.
  104. */
  105. void nds32_dcache_invalidate_range(unsigned long start, unsigned long end){
  106. #ifdef CONFIG_CPU_DCACHE_ENABLE
  107. unsigned long line_size;
  108. line_size = CACHE_LINE_SIZE(DCACHE);
  109. chk_range_alignment(start, end, line_size);
  110. while (end > start){
  111. __nds32__cctlva_wbinval_one_lvl(NDS32_CCTL_L1D_VA_INVAL, (void *)start);
  112. start += line_size;
  113. }
  114. #endif
  115. }
  116. void nds32_dcache_flush_range(unsigned long start, unsigned long end){
  117. #ifdef CONFIG_CPU_DCACHE_ENABLE
  118. unsigned long line_size;
  119. line_size = CACHE_LINE_SIZE(DCACHE);
  120. while (end > start){
  121. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  122. __nds32__cctlva_wbinval_one_lvl(NDS32_CCTL_L1D_VA_WB, (void *)start);
  123. #endif
  124. __nds32__cctlva_wbinval_one_lvl(NDS32_CCTL_L1D_VA_INVAL, (void *)start);
  125. start += line_size;
  126. }
  127. #endif
  128. }
  129. void nds32_dcache_writeback_range(unsigned long start, unsigned long end){
  130. #ifdef CONFIG_CPU_DCACHE_ENABLE
  131. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  132. unsigned long line_size;
  133. line_size = CACHE_LINE_SIZE(DCACHE);
  134. while (end > start){
  135. __nds32__cctlva_wbinval_one_lvl(NDS32_CCTL_L1D_VA_WB, (void *)start);
  136. start += line_size;
  137. }
  138. #endif
  139. #endif
  140. }
  141. void unaligned_cache_line_move(unsigned char* src, unsigned char* dst, unsigned long len )
  142. {
  143. int i;
  144. unsigned char* src_p = (unsigned char*)src;
  145. unsigned char* dst_p = (unsigned char*)dst;
  146. for( i = 0 ;i < len; ++i)
  147. *(dst_p+i)=*(src_p+i);
  148. }
  149. void nds32_dma_inv_range(unsigned long start, unsigned long end){
  150. unsigned long line_size;
  151. unsigned long old_start=start;
  152. unsigned long old_end=end;
  153. line_size = CACHE_LINE_SIZE(DCACHE);
  154. unsigned char h_buf[line_size];
  155. unsigned char t_buf[line_size];
  156. memset((void*)h_buf,0,line_size);
  157. memset((void*)t_buf,0,line_size);
  158. start = start & (~(line_size-1));
  159. end = (end + line_size -1) & (~(line_size-1));
  160. if (start == end)
  161. return;
  162. if (start != old_start)
  163. {
  164. //nds32_dcache_flush_range(start, start + line_size);
  165. unaligned_cache_line_move((unsigned char*)start, h_buf, old_start - start);
  166. }
  167. if (end != old_end)
  168. {
  169. //nds32_dcache_flush_range(end - line_size ,end);
  170. unaligned_cache_line_move((unsigned char*)old_end, t_buf, end - old_end);
  171. }
  172. nds32_dcache_invalidate_range(start, end);
  173. //handle cache line unaligned problem
  174. if(start != old_start)
  175. unaligned_cache_line_move(h_buf,(unsigned char*)start, old_start - start);
  176. if( end != old_end )
  177. unaligned_cache_line_move(t_buf,(unsigned char*)old_end, end - old_end);
  178. }
  179. void nds32_dma_flush_range(unsigned long start, unsigned long end){
  180. unsigned long line_size;
  181. line_size = CACHE_LINE_SIZE(DCACHE);
  182. start = start & (~(line_size-1));
  183. end = (end + line_size -1 ) & (~(line_size-1));
  184. if (start == end)
  185. return;
  186. nds32_dcache_flush_range(start, end);
  187. }
  188. /* ================================ I-CACHE =============================== */
  189. /*
  190. * nds32_icache_invalidate_range(start, end)
  191. *
  192. * invalidate a range of virtual addresses from the Icache
  193. *
  194. * This is a little misleading, it is not intended to clean out
  195. * the i-cache but to make sure that any data written to the
  196. * range is made consistant. This means that when we execute code
  197. * in that region, everything works as we expect.
  198. *
  199. * This generally means writing back data in the Dcache and
  200. * write buffer and flushing the Icache over that region
  201. *
  202. * start: virtual start address
  203. * end: virtual end address
  204. */
  205. void nds32_icache_invalidate_range(unsigned long start, unsigned long end){
  206. #ifdef CONFIG_CPU_ICACHE_ENABLE
  207. unsigned long line_size;
  208. line_size = CACHE_LINE_SIZE(ICACHE);
  209. //chk_range_alignment(start, end, line_size);
  210. start &= (~(line_size-1));
  211. end = ( end + line_size - 1 )&(~(line_size-1));
  212. if (end == start)
  213. end += line_size;
  214. while (end > start){
  215. end -= line_size;
  216. __nds32__cctlva_wbinval_one_lvl(NDS32_CCTL_L1I_VA_INVAL, (void *)end);
  217. }
  218. #endif
  219. }