cache_v8.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * (C) Copyright 2013
  3. * David Feng <fenghua@phytium.com.cn>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/system.h>
  9. #include <asm/armv8/mmu.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. #ifndef CONFIG_SYS_DCACHE_OFF
  12. void set_pgtable_section(u64 *page_table, u64 index, u64 section,
  13. u64 memory_type)
  14. {
  15. u64 value;
  16. value = section | PMD_TYPE_SECT | PMD_SECT_AF;
  17. value |= PMD_ATTRINDX(memory_type);
  18. page_table[index] = value;
  19. }
  20. /* to activate the MMU we need to set up virtual memory */
  21. static void mmu_setup(void)
  22. {
  23. int i, j, el;
  24. bd_t *bd = gd->bd;
  25. u64 *page_table = (u64 *)gd->arch.tlb_addr;
  26. /* Setup an identity-mapping for all spaces */
  27. for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
  28. set_pgtable_section(page_table, i, i << SECTION_SHIFT,
  29. MT_DEVICE_NGNRNE);
  30. }
  31. /* Setup an identity-mapping for all RAM space */
  32. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  33. ulong start = bd->bi_dram[i].start;
  34. ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
  35. for (j = start >> SECTION_SHIFT;
  36. j < end >> SECTION_SHIFT; j++) {
  37. set_pgtable_section(page_table, j, j << SECTION_SHIFT,
  38. MT_NORMAL);
  39. }
  40. }
  41. /* load TTBR0 */
  42. el = current_el();
  43. if (el == 1) {
  44. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  45. TCR_FLAGS | TCR_EL1_IPS_BITS,
  46. MEMORY_ATTRIBUTES);
  47. } else if (el == 2) {
  48. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  49. TCR_FLAGS | TCR_EL2_IPS_BITS,
  50. MEMORY_ATTRIBUTES);
  51. } else {
  52. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  53. TCR_FLAGS | TCR_EL3_IPS_BITS,
  54. MEMORY_ATTRIBUTES);
  55. }
  56. /* enable the mmu */
  57. set_sctlr(get_sctlr() | CR_M);
  58. }
  59. /*
  60. * Performs a invalidation of the entire data cache at all levels
  61. */
  62. void invalidate_dcache_all(void)
  63. {
  64. __asm_invalidate_dcache_all();
  65. }
  66. void __weak flush_l3_cache(void)
  67. {
  68. }
  69. /*
  70. * Performs a clean & invalidation of the entire data cache at all levels
  71. */
  72. void flush_dcache_all(void)
  73. {
  74. __asm_flush_dcache_all();
  75. flush_l3_cache();
  76. }
  77. /*
  78. * Invalidates range in all levels of D-cache/unified cache
  79. */
  80. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  81. {
  82. __asm_flush_dcache_range(start, stop);
  83. }
  84. /*
  85. * Flush range(clean & invalidate) from all levels of D-cache/unified cache
  86. */
  87. void flush_dcache_range(unsigned long start, unsigned long stop)
  88. {
  89. __asm_flush_dcache_range(start, stop);
  90. }
  91. void dcache_enable(void)
  92. {
  93. /* The data cache is not active unless the mmu is enabled */
  94. if (!(get_sctlr() & CR_M)) {
  95. invalidate_dcache_all();
  96. __asm_invalidate_tlb_all();
  97. mmu_setup();
  98. }
  99. set_sctlr(get_sctlr() | CR_C);
  100. }
  101. void dcache_disable(void)
  102. {
  103. uint32_t sctlr;
  104. sctlr = get_sctlr();
  105. /* if cache isn't enabled no need to disable */
  106. if (!(sctlr & CR_C))
  107. return;
  108. set_sctlr(sctlr & ~(CR_C|CR_M));
  109. flush_dcache_all();
  110. __asm_invalidate_tlb_all();
  111. }
  112. int dcache_status(void)
  113. {
  114. return (get_sctlr() & CR_C) != 0;
  115. }
  116. #else /* CONFIG_SYS_DCACHE_OFF */
  117. void invalidate_dcache_all(void)
  118. {
  119. }
  120. void flush_dcache_all(void)
  121. {
  122. }
  123. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  124. {
  125. }
  126. void flush_dcache_range(unsigned long start, unsigned long stop)
  127. {
  128. }
  129. void dcache_enable(void)
  130. {
  131. }
  132. void dcache_disable(void)
  133. {
  134. }
  135. int dcache_status(void)
  136. {
  137. return 0;
  138. }
  139. #endif /* CONFIG_SYS_DCACHE_OFF */
  140. #ifndef CONFIG_SYS_ICACHE_OFF
  141. void icache_enable(void)
  142. {
  143. __asm_invalidate_icache_all();
  144. set_sctlr(get_sctlr() | CR_I);
  145. }
  146. void icache_disable(void)
  147. {
  148. set_sctlr(get_sctlr() & ~CR_I);
  149. }
  150. int icache_status(void)
  151. {
  152. return (get_sctlr() & CR_I) != 0;
  153. }
  154. void invalidate_icache_all(void)
  155. {
  156. __asm_invalidate_icache_all();
  157. }
  158. #else /* CONFIG_SYS_ICACHE_OFF */
  159. void icache_enable(void)
  160. {
  161. }
  162. void icache_disable(void)
  163. {
  164. }
  165. int icache_status(void)
  166. {
  167. return 0;
  168. }
  169. void invalidate_icache_all(void)
  170. {
  171. }
  172. #endif /* CONFIG_SYS_ICACHE_OFF */
  173. /*
  174. * Enable dCache & iCache, whether cache is actually enabled
  175. * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
  176. */
  177. void __weak enable_caches(void)
  178. {
  179. icache_enable();
  180. dcache_enable();
  181. }
  182. /*
  183. * Flush range from all levels of d-cache/unified-cache
  184. */
  185. void flush_cache(unsigned long start, unsigned long size)
  186. {
  187. flush_dcache_range(start, start + size);
  188. }