cache.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  5. */
  6. #include <common.h>
  7. #include <asm/cacheops.h>
  8. #ifdef CONFIG_MIPS_L2_CACHE
  9. #include <asm/cm.h>
  10. #endif
  11. #include <asm/io.h>
  12. #include <asm/mipsregs.h>
  13. #include <asm/system.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static void probe_l2(void)
  16. {
  17. #ifdef CONFIG_MIPS_L2_CACHE
  18. unsigned long conf2, sl;
  19. bool l2c = false;
  20. if (!(read_c0_config1() & MIPS_CONF_M))
  21. return;
  22. conf2 = read_c0_config2();
  23. if (__mips_isa_rev >= 6) {
  24. l2c = conf2 & MIPS_CONF_M;
  25. if (l2c)
  26. l2c = read_c0_config3() & MIPS_CONF_M;
  27. if (l2c)
  28. l2c = read_c0_config4() & MIPS_CONF_M;
  29. if (l2c)
  30. l2c = read_c0_config5() & MIPS_CONF5_L2C;
  31. }
  32. if (l2c && config_enabled(CONFIG_MIPS_CM)) {
  33. gd->arch.l2_line_size = mips_cm_l2_line_size();
  34. } else if (l2c) {
  35. /* We don't know how to retrieve L2 config on this system */
  36. BUG();
  37. } else {
  38. sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF;
  39. gd->arch.l2_line_size = sl ? (2 << sl) : 0;
  40. }
  41. #endif
  42. }
  43. void mips_cache_probe(void)
  44. {
  45. #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
  46. unsigned long conf1, il, dl;
  47. conf1 = read_c0_config1();
  48. il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
  49. dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
  50. gd->arch.l1i_line_size = il ? (2 << il) : 0;
  51. gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
  52. #endif
  53. probe_l2();
  54. }
  55. static inline unsigned long icache_line_size(void)
  56. {
  57. #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
  58. return gd->arch.l1i_line_size;
  59. #else
  60. return CONFIG_SYS_ICACHE_LINE_SIZE;
  61. #endif
  62. }
  63. static inline unsigned long dcache_line_size(void)
  64. {
  65. #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
  66. return gd->arch.l1d_line_size;
  67. #else
  68. return CONFIG_SYS_DCACHE_LINE_SIZE;
  69. #endif
  70. }
  71. static inline unsigned long scache_line_size(void)
  72. {
  73. #ifdef CONFIG_MIPS_L2_CACHE
  74. return gd->arch.l2_line_size;
  75. #else
  76. return 0;
  77. #endif
  78. }
  79. #define cache_loop(start, end, lsize, ops...) do { \
  80. const void *addr = (const void *)(start & ~(lsize - 1)); \
  81. const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
  82. const unsigned int cache_ops[] = { ops }; \
  83. unsigned int i; \
  84. \
  85. if (!lsize) \
  86. break; \
  87. \
  88. for (; addr <= aend; addr += lsize) { \
  89. for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
  90. mips_cache(cache_ops[i], addr); \
  91. } \
  92. } while (0)
  93. void flush_cache(ulong start_addr, ulong size)
  94. {
  95. unsigned long ilsize = icache_line_size();
  96. unsigned long dlsize = dcache_line_size();
  97. unsigned long slsize = scache_line_size();
  98. /* aend will be miscalculated when size is zero, so we return here */
  99. if (size == 0)
  100. return;
  101. if ((ilsize == dlsize) && !slsize) {
  102. /* flush I-cache & D-cache simultaneously */
  103. cache_loop(start_addr, start_addr + size, ilsize,
  104. HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
  105. goto ops_done;
  106. }
  107. /* flush D-cache */
  108. cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
  109. /* flush L2 cache */
  110. cache_loop(start_addr, start_addr + size, slsize, HIT_WRITEBACK_INV_SD);
  111. /* flush I-cache */
  112. cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
  113. ops_done:
  114. /* ensure cache ops complete before any further memory accesses */
  115. sync();
  116. /* ensure the pipeline doesn't contain now-invalid instructions */
  117. instruction_hazard_barrier();
  118. }
  119. void flush_dcache_range(ulong start_addr, ulong stop)
  120. {
  121. unsigned long lsize = dcache_line_size();
  122. unsigned long slsize = scache_line_size();
  123. /* aend will be miscalculated when size is zero, so we return here */
  124. if (start_addr == stop)
  125. return;
  126. cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
  127. /* flush L2 cache */
  128. cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
  129. /* ensure cache ops complete before any further memory accesses */
  130. sync();
  131. }
  132. void invalidate_dcache_range(ulong start_addr, ulong stop)
  133. {
  134. unsigned long lsize = dcache_line_size();
  135. unsigned long slsize = scache_line_size();
  136. /* aend will be miscalculated when size is zero, so we return here */
  137. if (start_addr == stop)
  138. return;
  139. /* invalidate L2 cache */
  140. cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
  141. cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
  142. /* ensure cache ops complete before any further memory accesses */
  143. sync();
  144. }
  145. int dcache_status(void)
  146. {
  147. unsigned int cca = read_c0_config() & CONF_CM_CMASK;
  148. return cca != CONF_CM_UNCACHED;
  149. }
  150. void dcache_enable(void)
  151. {
  152. puts("Not supported!\n");
  153. }
  154. void dcache_disable(void)
  155. {
  156. /* change CCA to uncached */
  157. change_c0_config(CONF_CM_CMASK, CONF_CM_UNCACHED);
  158. /* ensure the pipeline doesn't contain now-invalid instructions */
  159. instruction_hazard_barrier();
  160. }