cache_v8.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. bd_t *bd = gd->bd;
  24. u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
  25. int el;
  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. /*
  67. * Performs a clean & invalidation of the entire data cache at all levels.
  68. * This function needs to be inline to avoid using stack.
  69. * __asm_flush_l3_cache return status of timeout
  70. */
  71. inline void flush_dcache_all(void)
  72. {
  73. int ret;
  74. __asm_flush_dcache_all();
  75. ret = __asm_flush_l3_cache();
  76. if (ret)
  77. debug("flushing dcache returns 0x%x\n", ret);
  78. else
  79. debug("flushing dcache successfully.\n");
  80. }
  81. /*
  82. * Invalidates range in all levels of D-cache/unified cache
  83. */
  84. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  85. {
  86. __asm_flush_dcache_range(start, stop);
  87. }
  88. /*
  89. * Flush range(clean & invalidate) from all levels of D-cache/unified cache
  90. */
  91. void flush_dcache_range(unsigned long start, unsigned long stop)
  92. {
  93. __asm_flush_dcache_range(start, stop);
  94. }
  95. void dcache_enable(void)
  96. {
  97. /* The data cache is not active unless the mmu is enabled */
  98. if (!(get_sctlr() & CR_M)) {
  99. invalidate_dcache_all();
  100. __asm_invalidate_tlb_all();
  101. mmu_setup();
  102. }
  103. set_sctlr(get_sctlr() | CR_C);
  104. }
  105. void dcache_disable(void)
  106. {
  107. uint32_t sctlr;
  108. sctlr = get_sctlr();
  109. /* if cache isn't enabled no need to disable */
  110. if (!(sctlr & CR_C))
  111. return;
  112. set_sctlr(sctlr & ~(CR_C|CR_M));
  113. flush_dcache_all();
  114. __asm_invalidate_tlb_all();
  115. }
  116. int dcache_status(void)
  117. {
  118. return (get_sctlr() & CR_C) != 0;
  119. }
  120. u64 *__weak arch_get_page_table(void) {
  121. puts("No page table offset defined\n");
  122. return NULL;
  123. }
  124. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  125. enum dcache_option option)
  126. {
  127. u64 *page_table = arch_get_page_table();
  128. u64 upto, end;
  129. if (page_table == NULL)
  130. return;
  131. end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
  132. MMU_SECTION_SHIFT;
  133. start = start >> MMU_SECTION_SHIFT;
  134. for (upto = start; upto < end; upto++) {
  135. page_table[upto] &= ~PMD_ATTRINDX_MASK;
  136. page_table[upto] |= PMD_ATTRINDX(option);
  137. }
  138. asm volatile("dsb sy");
  139. __asm_invalidate_tlb_all();
  140. asm volatile("dsb sy");
  141. asm volatile("isb");
  142. start = start << MMU_SECTION_SHIFT;
  143. end = end << MMU_SECTION_SHIFT;
  144. flush_dcache_range(start, end);
  145. asm volatile("dsb sy");
  146. }
  147. #else /* CONFIG_SYS_DCACHE_OFF */
  148. void invalidate_dcache_all(void)
  149. {
  150. }
  151. void flush_dcache_all(void)
  152. {
  153. }
  154. void dcache_enable(void)
  155. {
  156. }
  157. void dcache_disable(void)
  158. {
  159. }
  160. int dcache_status(void)
  161. {
  162. return 0;
  163. }
  164. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  165. enum dcache_option option)
  166. {
  167. }
  168. #endif /* CONFIG_SYS_DCACHE_OFF */
  169. #ifndef CONFIG_SYS_ICACHE_OFF
  170. void icache_enable(void)
  171. {
  172. __asm_invalidate_icache_all();
  173. set_sctlr(get_sctlr() | CR_I);
  174. }
  175. void icache_disable(void)
  176. {
  177. set_sctlr(get_sctlr() & ~CR_I);
  178. }
  179. int icache_status(void)
  180. {
  181. return (get_sctlr() & CR_I) != 0;
  182. }
  183. void invalidate_icache_all(void)
  184. {
  185. __asm_invalidate_icache_all();
  186. }
  187. #else /* CONFIG_SYS_ICACHE_OFF */
  188. void icache_enable(void)
  189. {
  190. }
  191. void icache_disable(void)
  192. {
  193. }
  194. int icache_status(void)
  195. {
  196. return 0;
  197. }
  198. void invalidate_icache_all(void)
  199. {
  200. }
  201. #endif /* CONFIG_SYS_ICACHE_OFF */
  202. /*
  203. * Enable dCache & iCache, whether cache is actually enabled
  204. * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
  205. */
  206. void __weak enable_caches(void)
  207. {
  208. icache_enable();
  209. dcache_enable();
  210. }