cache.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/cacheops.h>
  9. #ifdef CONFIG_MIPS_L2_CACHE
  10. #include <asm/cm.h>
  11. #endif
  12. #include <asm/io.h>
  13. #include <asm/mipsregs.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. for (; addr <= aend; addr += lsize) { \
  86. for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
  87. mips_cache(cache_ops[i], addr); \
  88. } \
  89. } while (0)
  90. void flush_cache(ulong start_addr, ulong size)
  91. {
  92. unsigned long ilsize = icache_line_size();
  93. unsigned long dlsize = dcache_line_size();
  94. unsigned long slsize = scache_line_size();
  95. /* aend will be miscalculated when size is zero, so we return here */
  96. if (size == 0)
  97. return;
  98. if ((ilsize == dlsize) && !slsize) {
  99. /* flush I-cache & D-cache simultaneously */
  100. cache_loop(start_addr, start_addr + size, ilsize,
  101. HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
  102. goto ops_done;
  103. }
  104. /* flush D-cache */
  105. cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
  106. /* flush L2 cache */
  107. if (slsize)
  108. cache_loop(start_addr, start_addr + size, slsize,
  109. HIT_WRITEBACK_INV_SD);
  110. /* flush I-cache */
  111. cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
  112. ops_done:
  113. /* ensure cache ops complete before any further memory accesses */
  114. sync();
  115. }
  116. void flush_dcache_range(ulong start_addr, ulong stop)
  117. {
  118. unsigned long lsize = dcache_line_size();
  119. unsigned long slsize = scache_line_size();
  120. /* aend will be miscalculated when size is zero, so we return here */
  121. if (start_addr == stop)
  122. return;
  123. cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
  124. /* flush L2 cache */
  125. if (slsize)
  126. cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
  127. /* ensure cache ops complete before any further memory accesses */
  128. sync();
  129. }
  130. void invalidate_dcache_range(ulong start_addr, ulong stop)
  131. {
  132. unsigned long lsize = dcache_line_size();
  133. unsigned long slsize = scache_line_size();
  134. /* aend will be miscalculated when size is zero, so we return here */
  135. if (start_addr == stop)
  136. return;
  137. /* invalidate L2 cache */
  138. if (slsize)
  139. cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
  140. cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
  141. /* ensure cache ops complete before any further memory accesses */
  142. sync();
  143. }