cache_v8.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifdef CONFIG_SYS_FULL_VA
  13. static void set_ptl1_entry(u64 index, u64 ptl2_entry)
  14. {
  15. u64 *pgd = (u64 *)gd->arch.tlb_addr;
  16. u64 value;
  17. value = ptl2_entry | PTL1_TYPE_TABLE;
  18. pgd[index] = value;
  19. }
  20. static void set_ptl2_block(u64 ptl1, u64 bfn, u64 address, u64 memory_attrs)
  21. {
  22. u64 *pmd = (u64 *)ptl1;
  23. u64 value;
  24. value = address | PTL2_TYPE_BLOCK | PTL2_BLOCK_AF;
  25. value |= memory_attrs;
  26. pmd[bfn] = value;
  27. }
  28. static struct mm_region mem_map[] = CONFIG_SYS_MEM_MAP;
  29. #define PTL1_ENTRIES CONFIG_SYS_PTL1_ENTRIES
  30. #define PTL2_ENTRIES CONFIG_SYS_PTL2_ENTRIES
  31. static void setup_pgtables(void)
  32. {
  33. int l1_e, l2_e;
  34. unsigned long pmd = 0;
  35. unsigned long address;
  36. /* Setup the PMD pointers */
  37. for (l1_e = 0; l1_e < CONFIG_SYS_MEM_MAP_SIZE; l1_e++) {
  38. gd->arch.pmd_addr[l1_e] = gd->arch.tlb_addr +
  39. PTL1_ENTRIES * sizeof(u64);
  40. gd->arch.pmd_addr[l1_e] += PTL2_ENTRIES * sizeof(u64) * l1_e;
  41. gd->arch.pmd_addr[l1_e] = ALIGN(gd->arch.pmd_addr[l1_e],
  42. 0x10000UL);
  43. }
  44. /* Setup the page tables */
  45. for (l1_e = 0; l1_e < PTL1_ENTRIES; l1_e++) {
  46. if (mem_map[pmd].base ==
  47. (uintptr_t)l1_e << PTL2_BITS) {
  48. set_ptl1_entry(l1_e, gd->arch.pmd_addr[pmd]);
  49. for (l2_e = 0; l2_e < PTL2_ENTRIES; l2_e++) {
  50. address = mem_map[pmd].base
  51. + (uintptr_t)l2_e * BLOCK_SIZE;
  52. set_ptl2_block(gd->arch.pmd_addr[pmd], l2_e,
  53. address, mem_map[pmd].attrs);
  54. }
  55. pmd++;
  56. } else {
  57. set_ptl1_entry(l1_e, 0);
  58. }
  59. }
  60. }
  61. #else
  62. inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
  63. u64 memory_type, u64 attribute)
  64. {
  65. u64 value;
  66. value = section | PMD_TYPE_SECT | PMD_SECT_AF;
  67. value |= PMD_ATTRINDX(memory_type);
  68. value |= attribute;
  69. page_table[index] = value;
  70. }
  71. inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
  72. {
  73. u64 value;
  74. value = (u64)table_addr | PMD_TYPE_TABLE;
  75. page_table[index] = value;
  76. }
  77. #endif
  78. /* to activate the MMU we need to set up virtual memory */
  79. __weak void mmu_setup(void)
  80. {
  81. #ifndef CONFIG_SYS_FULL_VA
  82. bd_t *bd = gd->bd;
  83. u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
  84. #endif
  85. int el;
  86. #ifdef CONFIG_SYS_FULL_VA
  87. unsigned long coreid = read_mpidr() & CONFIG_COREID_MASK;
  88. /* Set up page tables only on BSP */
  89. if (coreid == BSP_COREID)
  90. setup_pgtables();
  91. #else
  92. /* Setup an identity-mapping for all spaces */
  93. for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
  94. set_pgtable_section(page_table, i, i << SECTION_SHIFT,
  95. MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
  96. }
  97. /* Setup an identity-mapping for all RAM space */
  98. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  99. ulong start = bd->bi_dram[i].start;
  100. ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
  101. for (j = start >> SECTION_SHIFT;
  102. j < end >> SECTION_SHIFT; j++) {
  103. set_pgtable_section(page_table, j, j << SECTION_SHIFT,
  104. MT_NORMAL, PMD_SECT_NON_SHARE);
  105. }
  106. }
  107. #endif
  108. /* load TTBR0 */
  109. el = current_el();
  110. if (el == 1) {
  111. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  112. TCR_EL1_RSVD | TCR_FLAGS | TCR_EL1_IPS_BITS,
  113. MEMORY_ATTRIBUTES);
  114. } else if (el == 2) {
  115. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  116. TCR_EL2_RSVD | TCR_FLAGS | TCR_EL2_IPS_BITS,
  117. MEMORY_ATTRIBUTES);
  118. } else {
  119. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  120. TCR_EL3_RSVD | TCR_FLAGS | TCR_EL3_IPS_BITS,
  121. MEMORY_ATTRIBUTES);
  122. }
  123. /* enable the mmu */
  124. set_sctlr(get_sctlr() | CR_M);
  125. }
  126. /*
  127. * Performs a invalidation of the entire data cache at all levels
  128. */
  129. void invalidate_dcache_all(void)
  130. {
  131. __asm_invalidate_dcache_all();
  132. }
  133. /*
  134. * Performs a clean & invalidation of the entire data cache at all levels.
  135. * This function needs to be inline to avoid using stack.
  136. * __asm_flush_l3_cache return status of timeout
  137. */
  138. inline void flush_dcache_all(void)
  139. {
  140. int ret;
  141. __asm_flush_dcache_all();
  142. ret = __asm_flush_l3_cache();
  143. if (ret)
  144. debug("flushing dcache returns 0x%x\n", ret);
  145. else
  146. debug("flushing dcache successfully.\n");
  147. }
  148. /*
  149. * Invalidates range in all levels of D-cache/unified cache
  150. */
  151. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  152. {
  153. __asm_flush_dcache_range(start, stop);
  154. }
  155. /*
  156. * Flush range(clean & invalidate) from all levels of D-cache/unified cache
  157. */
  158. void flush_dcache_range(unsigned long start, unsigned long stop)
  159. {
  160. __asm_flush_dcache_range(start, stop);
  161. }
  162. void dcache_enable(void)
  163. {
  164. /* The data cache is not active unless the mmu is enabled */
  165. if (!(get_sctlr() & CR_M)) {
  166. invalidate_dcache_all();
  167. __asm_invalidate_tlb_all();
  168. mmu_setup();
  169. }
  170. set_sctlr(get_sctlr() | CR_C);
  171. }
  172. void dcache_disable(void)
  173. {
  174. uint32_t sctlr;
  175. sctlr = get_sctlr();
  176. /* if cache isn't enabled no need to disable */
  177. if (!(sctlr & CR_C))
  178. return;
  179. set_sctlr(sctlr & ~(CR_C|CR_M));
  180. flush_dcache_all();
  181. __asm_invalidate_tlb_all();
  182. }
  183. int dcache_status(void)
  184. {
  185. return (get_sctlr() & CR_C) != 0;
  186. }
  187. u64 *__weak arch_get_page_table(void) {
  188. puts("No page table offset defined\n");
  189. return NULL;
  190. }
  191. #ifndef CONFIG_SYS_FULL_VA
  192. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  193. enum dcache_option option)
  194. {
  195. u64 *page_table = arch_get_page_table();
  196. u64 upto, end;
  197. if (page_table == NULL)
  198. return;
  199. end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
  200. MMU_SECTION_SHIFT;
  201. start = start >> MMU_SECTION_SHIFT;
  202. for (upto = start; upto < end; upto++) {
  203. page_table[upto] &= ~PMD_ATTRINDX_MASK;
  204. page_table[upto] |= PMD_ATTRINDX(option);
  205. }
  206. asm volatile("dsb sy");
  207. __asm_invalidate_tlb_all();
  208. asm volatile("dsb sy");
  209. asm volatile("isb");
  210. start = start << MMU_SECTION_SHIFT;
  211. end = end << MMU_SECTION_SHIFT;
  212. flush_dcache_range(start, end);
  213. asm volatile("dsb sy");
  214. }
  215. #endif
  216. #else /* CONFIG_SYS_DCACHE_OFF */
  217. void invalidate_dcache_all(void)
  218. {
  219. }
  220. void flush_dcache_all(void)
  221. {
  222. }
  223. void dcache_enable(void)
  224. {
  225. }
  226. void dcache_disable(void)
  227. {
  228. }
  229. int dcache_status(void)
  230. {
  231. return 0;
  232. }
  233. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  234. enum dcache_option option)
  235. {
  236. }
  237. #endif /* CONFIG_SYS_DCACHE_OFF */
  238. #ifndef CONFIG_SYS_ICACHE_OFF
  239. void icache_enable(void)
  240. {
  241. __asm_invalidate_icache_all();
  242. set_sctlr(get_sctlr() | CR_I);
  243. }
  244. void icache_disable(void)
  245. {
  246. set_sctlr(get_sctlr() & ~CR_I);
  247. }
  248. int icache_status(void)
  249. {
  250. return (get_sctlr() & CR_I) != 0;
  251. }
  252. void invalidate_icache_all(void)
  253. {
  254. __asm_invalidate_icache_all();
  255. }
  256. #else /* CONFIG_SYS_ICACHE_OFF */
  257. void icache_enable(void)
  258. {
  259. }
  260. void icache_disable(void)
  261. {
  262. }
  263. int icache_status(void)
  264. {
  265. return 0;
  266. }
  267. void invalidate_icache_all(void)
  268. {
  269. }
  270. #endif /* CONFIG_SYS_ICACHE_OFF */
  271. /*
  272. * Enable dCache & iCache, whether cache is actually enabled
  273. * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
  274. */
  275. void __weak enable_caches(void)
  276. {
  277. icache_enable();
  278. dcache_enable();
  279. }