cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/mipsregs.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static void probe_l2(void)
  15. {
  16. #ifdef CONFIG_MIPS_L2_CACHE
  17. unsigned long conf2, sl;
  18. bool l2c = false;
  19. if (!(read_c0_config1() & MIPS_CONF_M))
  20. return;
  21. conf2 = read_c0_config2();
  22. if (__mips_isa_rev >= 6) {
  23. l2c = conf2 & MIPS_CONF_M;
  24. if (l2c)
  25. l2c = read_c0_config3() & MIPS_CONF_M;
  26. if (l2c)
  27. l2c = read_c0_config4() & MIPS_CONF_M;
  28. if (l2c)
  29. l2c = read_c0_config5() & MIPS_CONF5_L2C;
  30. }
  31. if (l2c && config_enabled(CONFIG_MIPS_CM)) {
  32. gd->arch.l2_line_size = mips_cm_l2_line_size();
  33. } else if (l2c) {
  34. /* We don't know how to retrieve L2 config on this system */
  35. BUG();
  36. } else {
  37. sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF;
  38. gd->arch.l2_line_size = sl ? (2 << sl) : 0;
  39. }
  40. #endif
  41. }
  42. void mips_cache_probe(void)
  43. {
  44. #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
  45. unsigned long conf1, il, dl;
  46. conf1 = read_c0_config1();
  47. il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
  48. dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
  49. gd->arch.l1i_line_size = il ? (2 << il) : 0;
  50. gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
  51. #endif
  52. probe_l2();
  53. }
  54. static inline unsigned long icache_line_size(void)
  55. {
  56. #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
  57. return gd->arch.l1i_line_size;
  58. #else
  59. return CONFIG_SYS_ICACHE_LINE_SIZE;
  60. #endif
  61. }
  62. static inline unsigned long dcache_line_size(void)
  63. {
  64. #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
  65. return gd->arch.l1d_line_size;
  66. #else
  67. return CONFIG_SYS_DCACHE_LINE_SIZE;
  68. #endif
  69. }
  70. static inline unsigned long scache_line_size(void)
  71. {
  72. #ifdef CONFIG_MIPS_L2_CACHE
  73. return gd->arch.l2_line_size;
  74. #else
  75. return 0;
  76. #endif
  77. }
  78. #define cache_loop(start, end, lsize, ops...) do { \
  79. const void *addr = (const void *)(start & ~(lsize - 1)); \
  80. const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
  81. const unsigned int cache_ops[] = { ops }; \
  82. unsigned int i; \
  83. \
  84. for (; addr <= aend; addr += lsize) { \
  85. for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
  86. mips_cache(cache_ops[i], addr); \
  87. } \
  88. } while (0)
  89. void flush_cache(ulong start_addr, ulong size)
  90. {
  91. unsigned long ilsize = icache_line_size();
  92. unsigned long dlsize = dcache_line_size();
  93. unsigned long slsize = scache_line_size();
  94. /* aend will be miscalculated when size is zero, so we return here */
  95. if (size == 0)
  96. return;
  97. if ((ilsize == dlsize) && !slsize) {
  98. /* flush I-cache & D-cache simultaneously */
  99. cache_loop(start_addr, start_addr + size, ilsize,
  100. HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
  101. return;
  102. }
  103. /* flush D-cache */
  104. cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
  105. /* flush L2 cache */
  106. if (slsize)
  107. cache_loop(start_addr, start_addr + size, slsize,
  108. HIT_WRITEBACK_INV_SD);
  109. /* flush I-cache */
  110. cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
  111. }
  112. void flush_dcache_range(ulong start_addr, ulong stop)
  113. {
  114. unsigned long lsize = dcache_line_size();
  115. unsigned long slsize = scache_line_size();
  116. /* aend will be miscalculated when size is zero, so we return here */
  117. if (start_addr == stop)
  118. return;
  119. cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
  120. /* flush L2 cache */
  121. if (slsize)
  122. cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
  123. }
  124. void invalidate_dcache_range(ulong start_addr, ulong stop)
  125. {
  126. unsigned long lsize = dcache_line_size();
  127. unsigned long slsize = scache_line_size();
  128. /* aend will be miscalculated when size is zero, so we return here */
  129. if (start_addr == stop)
  130. return;
  131. /* invalidate L2 cache */
  132. if (slsize)
  133. cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
  134. cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
  135. }