cache.c 3.7 KB

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