cache_v8.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
  13. u64 memory_type, u64 share)
  14. {
  15. u64 value;
  16. value = section | PMD_TYPE_SECT | PMD_SECT_AF;
  17. value |= PMD_ATTRINDX(memory_type);
  18. value |= share;
  19. page_table[index] = value;
  20. }
  21. inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
  22. {
  23. u64 value;
  24. value = (u64)table_addr | PMD_TYPE_TABLE;
  25. page_table[index] = value;
  26. }
  27. /* to activate the MMU we need to set up virtual memory */
  28. static void mmu_setup(void)
  29. {
  30. bd_t *bd = gd->bd;
  31. u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
  32. int el;
  33. /* Setup an identity-mapping for all spaces */
  34. for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
  35. set_pgtable_section(page_table, i, i << SECTION_SHIFT,
  36. MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
  37. }
  38. /* Setup an identity-mapping for all RAM space */
  39. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  40. ulong start = bd->bi_dram[i].start;
  41. ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
  42. for (j = start >> SECTION_SHIFT;
  43. j < end >> SECTION_SHIFT; j++) {
  44. set_pgtable_section(page_table, j, j << SECTION_SHIFT,
  45. MT_NORMAL, PMD_SECT_NON_SHARE);
  46. }
  47. }
  48. /* load TTBR0 */
  49. el = current_el();
  50. if (el == 1) {
  51. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  52. TCR_EL1_RSVD | TCR_FLAGS | TCR_EL1_IPS_BITS,
  53. MEMORY_ATTRIBUTES);
  54. } else if (el == 2) {
  55. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  56. TCR_EL2_RSVD | TCR_FLAGS | TCR_EL2_IPS_BITS,
  57. MEMORY_ATTRIBUTES);
  58. } else {
  59. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  60. TCR_EL3_RSVD | TCR_FLAGS | TCR_EL3_IPS_BITS,
  61. MEMORY_ATTRIBUTES);
  62. }
  63. /* enable the mmu */
  64. set_sctlr(get_sctlr() | CR_M);
  65. }
  66. /*
  67. * Performs a invalidation of the entire data cache at all levels
  68. */
  69. void invalidate_dcache_all(void)
  70. {
  71. __asm_invalidate_dcache_all();
  72. }
  73. /*
  74. * Performs a clean & invalidation of the entire data cache at all levels.
  75. * This function needs to be inline to avoid using stack.
  76. * __asm_flush_l3_cache return status of timeout
  77. */
  78. inline void flush_dcache_all(void)
  79. {
  80. int ret;
  81. __asm_flush_dcache_all();
  82. ret = __asm_flush_l3_cache();
  83. if (ret)
  84. debug("flushing dcache returns 0x%x\n", ret);
  85. else
  86. debug("flushing dcache successfully.\n");
  87. }
  88. /*
  89. * Invalidates range in all levels of D-cache/unified cache
  90. */
  91. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  92. {
  93. __asm_flush_dcache_range(start, stop);
  94. }
  95. /*
  96. * Flush range(clean & invalidate) from all levels of D-cache/unified cache
  97. */
  98. void flush_dcache_range(unsigned long start, unsigned long stop)
  99. {
  100. __asm_flush_dcache_range(start, stop);
  101. }
  102. void dcache_enable(void)
  103. {
  104. /* The data cache is not active unless the mmu is enabled */
  105. if (!(get_sctlr() & CR_M)) {
  106. invalidate_dcache_all();
  107. __asm_invalidate_tlb_all();
  108. mmu_setup();
  109. }
  110. set_sctlr(get_sctlr() | CR_C);
  111. }
  112. void dcache_disable(void)
  113. {
  114. uint32_t sctlr;
  115. sctlr = get_sctlr();
  116. /* if cache isn't enabled no need to disable */
  117. if (!(sctlr & CR_C))
  118. return;
  119. set_sctlr(sctlr & ~(CR_C|CR_M));
  120. flush_dcache_all();
  121. __asm_invalidate_tlb_all();
  122. }
  123. int dcache_status(void)
  124. {
  125. return (get_sctlr() & CR_C) != 0;
  126. }
  127. u64 *__weak arch_get_page_table(void) {
  128. puts("No page table offset defined\n");
  129. return NULL;
  130. }
  131. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  132. enum dcache_option option)
  133. {
  134. u64 *page_table = arch_get_page_table();
  135. u64 upto, end;
  136. if (page_table == NULL)
  137. return;
  138. end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
  139. MMU_SECTION_SHIFT;
  140. start = start >> MMU_SECTION_SHIFT;
  141. for (upto = start; upto < end; upto++) {
  142. page_table[upto] &= ~PMD_ATTRINDX_MASK;
  143. page_table[upto] |= PMD_ATTRINDX(option);
  144. }
  145. asm volatile("dsb sy");
  146. __asm_invalidate_tlb_all();
  147. asm volatile("dsb sy");
  148. asm volatile("isb");
  149. start = start << MMU_SECTION_SHIFT;
  150. end = end << MMU_SECTION_SHIFT;
  151. flush_dcache_range(start, end);
  152. asm volatile("dsb sy");
  153. }
  154. #else /* CONFIG_SYS_DCACHE_OFF */
  155. void invalidate_dcache_all(void)
  156. {
  157. }
  158. void flush_dcache_all(void)
  159. {
  160. }
  161. void dcache_enable(void)
  162. {
  163. }
  164. void dcache_disable(void)
  165. {
  166. }
  167. int dcache_status(void)
  168. {
  169. return 0;
  170. }
  171. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  172. enum dcache_option option)
  173. {
  174. }
  175. #endif /* CONFIG_SYS_DCACHE_OFF */
  176. #ifndef CONFIG_SYS_ICACHE_OFF
  177. void icache_enable(void)
  178. {
  179. __asm_invalidate_icache_all();
  180. set_sctlr(get_sctlr() | CR_I);
  181. }
  182. void icache_disable(void)
  183. {
  184. set_sctlr(get_sctlr() & ~CR_I);
  185. }
  186. int icache_status(void)
  187. {
  188. return (get_sctlr() & CR_I) != 0;
  189. }
  190. void invalidate_icache_all(void)
  191. {
  192. __asm_invalidate_icache_all();
  193. }
  194. #else /* CONFIG_SYS_ICACHE_OFF */
  195. void icache_enable(void)
  196. {
  197. }
  198. void icache_disable(void)
  199. {
  200. }
  201. int icache_status(void)
  202. {
  203. return 0;
  204. }
  205. void invalidate_icache_all(void)
  206. {
  207. }
  208. #endif /* CONFIG_SYS_ICACHE_OFF */
  209. /*
  210. * Enable dCache & iCache, whether cache is actually enabled
  211. * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
  212. */
  213. void __weak enable_caches(void)
  214. {
  215. icache_enable();
  216. dcache_enable();
  217. }