cache_v8.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * (C) Copyright 2013
  3. * David Feng <fenghua@phytium.com.cn>
  4. *
  5. * (C) Copyright 2016
  6. * Alexander Graf <agraf@suse.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <asm/system.h>
  12. #include <asm/armv8/mmu.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #ifndef CONFIG_SYS_DCACHE_OFF
  15. /*
  16. * With 4k page granule, a virtual address is split into 4 lookup parts
  17. * spanning 9 bits each:
  18. *
  19. * _______________________________________________
  20. * | | | | | | |
  21. * | 0 | Lv0 | Lv1 | Lv2 | Lv3 | off |
  22. * |_______|_______|_______|_______|_______|_______|
  23. * 63-48 47-39 38-30 29-21 20-12 11-00
  24. *
  25. * mask page size
  26. *
  27. * Lv0: FF8000000000 --
  28. * Lv1: 7FC0000000 1G
  29. * Lv2: 3FE00000 2M
  30. * Lv3: 1FF000 4K
  31. * off: FFF
  32. */
  33. u64 get_tcr(int el, u64 *pips, u64 *pva_bits)
  34. {
  35. u64 max_addr = 0;
  36. u64 ips, va_bits;
  37. u64 tcr;
  38. int i;
  39. /* Find the largest address we need to support */
  40. for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
  41. max_addr = max(max_addr, mem_map[i].virt + mem_map[i].size);
  42. /* Calculate the maximum physical (and thus virtual) address */
  43. if (max_addr > (1ULL << 44)) {
  44. ips = 5;
  45. va_bits = 48;
  46. } else if (max_addr > (1ULL << 42)) {
  47. ips = 4;
  48. va_bits = 44;
  49. } else if (max_addr > (1ULL << 40)) {
  50. ips = 3;
  51. va_bits = 42;
  52. } else if (max_addr > (1ULL << 36)) {
  53. ips = 2;
  54. va_bits = 40;
  55. } else if (max_addr > (1ULL << 32)) {
  56. ips = 1;
  57. va_bits = 36;
  58. } else {
  59. ips = 0;
  60. va_bits = 32;
  61. }
  62. if (el == 1) {
  63. tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
  64. } else if (el == 2) {
  65. tcr = TCR_EL2_RSVD | (ips << 16);
  66. } else {
  67. tcr = TCR_EL3_RSVD | (ips << 16);
  68. }
  69. /* PTWs cacheable, inner/outer WBWA and inner shareable */
  70. tcr |= TCR_TG0_4K | TCR_SHARED_INNER | TCR_ORGN_WBWA | TCR_IRGN_WBWA;
  71. tcr |= TCR_T0SZ(va_bits);
  72. if (pips)
  73. *pips = ips;
  74. if (pva_bits)
  75. *pva_bits = va_bits;
  76. return tcr;
  77. }
  78. #define MAX_PTE_ENTRIES 512
  79. static int pte_type(u64 *pte)
  80. {
  81. return *pte & PTE_TYPE_MASK;
  82. }
  83. /* Returns the LSB number for a PTE on level <level> */
  84. static int level2shift(int level)
  85. {
  86. /* Page is 12 bits wide, every level translates 9 bits */
  87. return (12 + 9 * (3 - level));
  88. }
  89. static u64 *find_pte(u64 addr, int level)
  90. {
  91. int start_level = 0;
  92. u64 *pte;
  93. u64 idx;
  94. u64 va_bits;
  95. int i;
  96. debug("addr=%llx level=%d\n", addr, level);
  97. get_tcr(0, NULL, &va_bits);
  98. if (va_bits < 39)
  99. start_level = 1;
  100. if (level < start_level)
  101. return NULL;
  102. /* Walk through all page table levels to find our PTE */
  103. pte = (u64*)gd->arch.tlb_addr;
  104. for (i = start_level; i < 4; i++) {
  105. idx = (addr >> level2shift(i)) & 0x1FF;
  106. pte += idx;
  107. debug("idx=%llx PTE %p at level %d: %llx\n", idx, pte, i, *pte);
  108. /* Found it */
  109. if (i == level)
  110. return pte;
  111. /* PTE is no table (either invalid or block), can't traverse */
  112. if (pte_type(pte) != PTE_TYPE_TABLE)
  113. return NULL;
  114. /* Off to the next level */
  115. pte = (u64*)(*pte & 0x0000fffffffff000ULL);
  116. }
  117. /* Should never reach here */
  118. return NULL;
  119. }
  120. /* Returns and creates a new full table (512 entries) */
  121. static u64 *create_table(void)
  122. {
  123. u64 *new_table = (u64*)gd->arch.tlb_fillptr;
  124. u64 pt_len = MAX_PTE_ENTRIES * sizeof(u64);
  125. /* Allocate MAX_PTE_ENTRIES pte entries */
  126. gd->arch.tlb_fillptr += pt_len;
  127. if (gd->arch.tlb_fillptr - gd->arch.tlb_addr > gd->arch.tlb_size)
  128. panic("Insufficient RAM for page table: 0x%lx > 0x%lx. "
  129. "Please increase the size in get_page_table_size()",
  130. gd->arch.tlb_fillptr - gd->arch.tlb_addr,
  131. gd->arch.tlb_size);
  132. /* Mark all entries as invalid */
  133. memset(new_table, 0, pt_len);
  134. return new_table;
  135. }
  136. static void set_pte_table(u64 *pte, u64 *table)
  137. {
  138. /* Point *pte to the new table */
  139. debug("Setting %p to addr=%p\n", pte, table);
  140. *pte = PTE_TYPE_TABLE | (ulong)table;
  141. }
  142. /* Splits a block PTE into table with subpages spanning the old block */
  143. static void split_block(u64 *pte, int level)
  144. {
  145. u64 old_pte = *pte;
  146. u64 *new_table;
  147. u64 i = 0;
  148. /* level describes the parent level, we need the child ones */
  149. int levelshift = level2shift(level + 1);
  150. if (pte_type(pte) != PTE_TYPE_BLOCK)
  151. panic("PTE %p (%llx) is not a block. Some driver code wants to "
  152. "modify dcache settings for an range not covered in "
  153. "mem_map.", pte, old_pte);
  154. new_table = create_table();
  155. debug("Splitting pte %p (%llx) into %p\n", pte, old_pte, new_table);
  156. for (i = 0; i < MAX_PTE_ENTRIES; i++) {
  157. new_table[i] = old_pte | (i << levelshift);
  158. /* Level 3 block PTEs have the table type */
  159. if ((level + 1) == 3)
  160. new_table[i] |= PTE_TYPE_TABLE;
  161. debug("Setting new_table[%lld] = %llx\n", i, new_table[i]);
  162. }
  163. /* Set the new table into effect */
  164. set_pte_table(pte, new_table);
  165. }
  166. /* Add one mm_region map entry to the page tables */
  167. static void add_map(struct mm_region *map)
  168. {
  169. u64 *pte;
  170. u64 virt = map->virt;
  171. u64 phys = map->phys;
  172. u64 size = map->size;
  173. u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
  174. u64 blocksize;
  175. int level;
  176. u64 *new_table;
  177. while (size) {
  178. pte = find_pte(virt, 0);
  179. if (pte && (pte_type(pte) == PTE_TYPE_FAULT)) {
  180. debug("Creating table for virt 0x%llx\n", virt);
  181. new_table = create_table();
  182. set_pte_table(pte, new_table);
  183. }
  184. for (level = 1; level < 4; level++) {
  185. pte = find_pte(virt, level);
  186. if (!pte)
  187. panic("pte not found\n");
  188. blocksize = 1ULL << level2shift(level);
  189. debug("Checking if pte fits for virt=%llx size=%llx blocksize=%llx\n",
  190. virt, size, blocksize);
  191. if (size >= blocksize && !(virt & (blocksize - 1))) {
  192. /* Page fits, create block PTE */
  193. debug("Setting PTE %p to block virt=%llx\n",
  194. pte, virt);
  195. *pte = phys | attrs;
  196. virt += blocksize;
  197. phys += blocksize;
  198. size -= blocksize;
  199. break;
  200. } else if (pte_type(pte) == PTE_TYPE_FAULT) {
  201. /* Page doesn't fit, create subpages */
  202. debug("Creating subtable for virt 0x%llx blksize=%llx\n",
  203. virt, blocksize);
  204. new_table = create_table();
  205. set_pte_table(pte, new_table);
  206. } else if (pte_type(pte) == PTE_TYPE_BLOCK) {
  207. debug("Split block into subtable for virt 0x%llx blksize=0x%llx\n",
  208. virt, blocksize);
  209. split_block(pte, level);
  210. }
  211. }
  212. }
  213. }
  214. enum pte_type {
  215. PTE_INVAL,
  216. PTE_BLOCK,
  217. PTE_LEVEL,
  218. };
  219. /*
  220. * This is a recursively called function to count the number of
  221. * page tables we need to cover a particular PTE range. If you
  222. * call this with level = -1 you basically get the full 48 bit
  223. * coverage.
  224. */
  225. static int count_required_pts(u64 addr, int level, u64 maxaddr)
  226. {
  227. int levelshift = level2shift(level);
  228. u64 levelsize = 1ULL << levelshift;
  229. u64 levelmask = levelsize - 1;
  230. u64 levelend = addr + levelsize;
  231. int r = 0;
  232. int i;
  233. enum pte_type pte_type = PTE_INVAL;
  234. for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) {
  235. struct mm_region *map = &mem_map[i];
  236. u64 start = map->virt;
  237. u64 end = start + map->size;
  238. /* Check if the PTE would overlap with the map */
  239. if (max(addr, start) <= min(levelend, end)) {
  240. start = max(addr, start);
  241. end = min(levelend, end);
  242. /* We need a sub-pt for this level */
  243. if ((start & levelmask) || (end & levelmask)) {
  244. pte_type = PTE_LEVEL;
  245. break;
  246. }
  247. /* Lv0 can not do block PTEs, so do levels here too */
  248. if (level <= 0) {
  249. pte_type = PTE_LEVEL;
  250. break;
  251. }
  252. /* PTE is active, but fits into a block */
  253. pte_type = PTE_BLOCK;
  254. }
  255. }
  256. /*
  257. * Block PTEs at this level are already covered by the parent page
  258. * table, so we only need to count sub page tables.
  259. */
  260. if (pte_type == PTE_LEVEL) {
  261. int sublevel = level + 1;
  262. u64 sublevelsize = 1ULL << level2shift(sublevel);
  263. /* Account for the new sub page table ... */
  264. r = 1;
  265. /* ... and for all child page tables that one might have */
  266. for (i = 0; i < MAX_PTE_ENTRIES; i++) {
  267. r += count_required_pts(addr, sublevel, maxaddr);
  268. addr += sublevelsize;
  269. if (addr >= maxaddr) {
  270. /*
  271. * We reached the end of address space, no need
  272. * to look any further.
  273. */
  274. break;
  275. }
  276. }
  277. }
  278. return r;
  279. }
  280. /* Returns the estimated required size of all page tables */
  281. __weak u64 get_page_table_size(void)
  282. {
  283. u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
  284. u64 size = 0;
  285. u64 va_bits;
  286. int start_level = 0;
  287. get_tcr(0, NULL, &va_bits);
  288. if (va_bits < 39)
  289. start_level = 1;
  290. /* Account for all page tables we would need to cover our memory map */
  291. size = one_pt * count_required_pts(0, start_level - 1, 1ULL << va_bits);
  292. /*
  293. * We need to duplicate our page table once to have an emergency pt to
  294. * resort to when splitting page tables later on
  295. */
  296. size *= 2;
  297. /*
  298. * We may need to split page tables later on if dcache settings change,
  299. * so reserve up to 4 (random pick) page tables for that.
  300. */
  301. size += one_pt * 4;
  302. return size;
  303. }
  304. void setup_pgtables(void)
  305. {
  306. int i;
  307. if (!gd->arch.tlb_fillptr || !gd->arch.tlb_addr)
  308. panic("Page table pointer not setup.");
  309. /*
  310. * Allocate the first level we're on with invalidate entries.
  311. * If the starting level is 0 (va_bits >= 39), then this is our
  312. * Lv0 page table, otherwise it's the entry Lv1 page table.
  313. */
  314. create_table();
  315. /* Now add all MMU table entries one after another to the table */
  316. for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
  317. add_map(&mem_map[i]);
  318. }
  319. static void setup_all_pgtables(void)
  320. {
  321. u64 tlb_addr = gd->arch.tlb_addr;
  322. u64 tlb_size = gd->arch.tlb_size;
  323. /* Reset the fill ptr */
  324. gd->arch.tlb_fillptr = tlb_addr;
  325. /* Create normal system page tables */
  326. setup_pgtables();
  327. /* Create emergency page tables */
  328. gd->arch.tlb_size -= (uintptr_t)gd->arch.tlb_fillptr -
  329. (uintptr_t)gd->arch.tlb_addr;
  330. gd->arch.tlb_addr = gd->arch.tlb_fillptr;
  331. setup_pgtables();
  332. gd->arch.tlb_emerg = gd->arch.tlb_addr;
  333. gd->arch.tlb_addr = tlb_addr;
  334. gd->arch.tlb_size = tlb_size;
  335. }
  336. /* to activate the MMU we need to set up virtual memory */
  337. __weak void mmu_setup(void)
  338. {
  339. int el;
  340. /* Set up page tables only once */
  341. if (!gd->arch.tlb_fillptr)
  342. setup_all_pgtables();
  343. el = current_el();
  344. set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
  345. MEMORY_ATTRIBUTES);
  346. /* enable the mmu */
  347. set_sctlr(get_sctlr() | CR_M);
  348. }
  349. /*
  350. * Performs a invalidation of the entire data cache at all levels
  351. */
  352. void invalidate_dcache_all(void)
  353. {
  354. __asm_invalidate_dcache_all();
  355. __asm_invalidate_l3_dcache();
  356. }
  357. /*
  358. * Performs a clean & invalidation of the entire data cache at all levels.
  359. * This function needs to be inline to avoid using stack.
  360. * __asm_flush_l3_dcache return status of timeout
  361. */
  362. inline void flush_dcache_all(void)
  363. {
  364. int ret;
  365. __asm_flush_dcache_all();
  366. ret = __asm_flush_l3_dcache();
  367. if (ret)
  368. debug("flushing dcache returns 0x%x\n", ret);
  369. else
  370. debug("flushing dcache successfully.\n");
  371. }
  372. /*
  373. * Invalidates range in all levels of D-cache/unified cache
  374. */
  375. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  376. {
  377. __asm_flush_dcache_range(start, stop);
  378. }
  379. /*
  380. * Flush range(clean & invalidate) from all levels of D-cache/unified cache
  381. */
  382. void flush_dcache_range(unsigned long start, unsigned long stop)
  383. {
  384. __asm_flush_dcache_range(start, stop);
  385. }
  386. void dcache_enable(void)
  387. {
  388. /* The data cache is not active unless the mmu is enabled */
  389. if (!(get_sctlr() & CR_M)) {
  390. invalidate_dcache_all();
  391. __asm_invalidate_tlb_all();
  392. mmu_setup();
  393. }
  394. set_sctlr(get_sctlr() | CR_C);
  395. }
  396. void dcache_disable(void)
  397. {
  398. uint32_t sctlr;
  399. sctlr = get_sctlr();
  400. /* if cache isn't enabled no need to disable */
  401. if (!(sctlr & CR_C))
  402. return;
  403. set_sctlr(sctlr & ~(CR_C|CR_M));
  404. flush_dcache_all();
  405. __asm_invalidate_tlb_all();
  406. }
  407. int dcache_status(void)
  408. {
  409. return (get_sctlr() & CR_C) != 0;
  410. }
  411. u64 *__weak arch_get_page_table(void) {
  412. puts("No page table offset defined\n");
  413. return NULL;
  414. }
  415. static bool is_aligned(u64 addr, u64 size, u64 align)
  416. {
  417. return !(addr & (align - 1)) && !(size & (align - 1));
  418. }
  419. static u64 set_one_region(u64 start, u64 size, u64 attrs, int level)
  420. {
  421. int levelshift = level2shift(level);
  422. u64 levelsize = 1ULL << levelshift;
  423. u64 *pte = find_pte(start, level);
  424. /* Can we can just modify the current level block PTE? */
  425. if (is_aligned(start, size, levelsize)) {
  426. *pte &= ~PMD_ATTRINDX_MASK;
  427. *pte |= attrs;
  428. debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
  429. return levelsize;
  430. }
  431. /* Unaligned or doesn't fit, maybe split block into table */
  432. debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
  433. /* Maybe we need to split the block into a table */
  434. if (pte_type(pte) == PTE_TYPE_BLOCK)
  435. split_block(pte, level);
  436. /* And then double-check it became a table or already is one */
  437. if (pte_type(pte) != PTE_TYPE_TABLE)
  438. panic("PTE %p (%llx) for addr=%llx should be a table",
  439. pte, *pte, start);
  440. /* Roll on to the next page table level */
  441. return 0;
  442. }
  443. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  444. enum dcache_option option)
  445. {
  446. u64 attrs = PMD_ATTRINDX(option);
  447. u64 real_start = start;
  448. u64 real_size = size;
  449. debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
  450. if (!gd->arch.tlb_emerg)
  451. panic("Emergency page table not setup.");
  452. /*
  453. * We can not modify page tables that we're currently running on,
  454. * so we first need to switch to the "emergency" page tables where
  455. * we can safely modify our primary page tables and then switch back
  456. */
  457. __asm_switch_ttbr(gd->arch.tlb_emerg);
  458. /*
  459. * Loop through the address range until we find a page granule that fits
  460. * our alignment constraints, then set it to the new cache attributes
  461. */
  462. while (size > 0) {
  463. int level;
  464. u64 r;
  465. for (level = 1; level < 4; level++) {
  466. r = set_one_region(start, size, attrs, level);
  467. if (r) {
  468. /* PTE successfully replaced */
  469. size -= r;
  470. start += r;
  471. break;
  472. }
  473. }
  474. }
  475. /* We're done modifying page tables, switch back to our primary ones */
  476. __asm_switch_ttbr(gd->arch.tlb_addr);
  477. /*
  478. * Make sure there's nothing stale in dcache for a region that might
  479. * have caches off now
  480. */
  481. flush_dcache_range(real_start, real_start + real_size);
  482. }
  483. #else /* CONFIG_SYS_DCACHE_OFF */
  484. /*
  485. * For SPL builds, we may want to not have dcache enabled. Any real U-Boot
  486. * running however really wants to have dcache and the MMU active. Check that
  487. * everything is sane and give the developer a hint if it isn't.
  488. */
  489. #ifndef CONFIG_SPL_BUILD
  490. #error Please describe your MMU layout in CONFIG_SYS_MEM_MAP and enable dcache.
  491. #endif
  492. void invalidate_dcache_all(void)
  493. {
  494. }
  495. void flush_dcache_all(void)
  496. {
  497. }
  498. void dcache_enable(void)
  499. {
  500. }
  501. void dcache_disable(void)
  502. {
  503. }
  504. int dcache_status(void)
  505. {
  506. return 0;
  507. }
  508. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  509. enum dcache_option option)
  510. {
  511. }
  512. #endif /* CONFIG_SYS_DCACHE_OFF */
  513. #ifndef CONFIG_SYS_ICACHE_OFF
  514. void icache_enable(void)
  515. {
  516. invalidate_icache_all();
  517. set_sctlr(get_sctlr() | CR_I);
  518. }
  519. void icache_disable(void)
  520. {
  521. set_sctlr(get_sctlr() & ~CR_I);
  522. }
  523. int icache_status(void)
  524. {
  525. return (get_sctlr() & CR_I) != 0;
  526. }
  527. void invalidate_icache_all(void)
  528. {
  529. __asm_invalidate_icache_all();
  530. __asm_invalidate_l3_icache();
  531. }
  532. #else /* CONFIG_SYS_ICACHE_OFF */
  533. void icache_enable(void)
  534. {
  535. }
  536. void icache_disable(void)
  537. {
  538. }
  539. int icache_status(void)
  540. {
  541. return 0;
  542. }
  543. void invalidate_icache_all(void)
  544. {
  545. }
  546. #endif /* CONFIG_SYS_ICACHE_OFF */
  547. /*
  548. * Enable dCache & iCache, whether cache is actually enabled
  549. * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
  550. */
  551. void __weak enable_caches(void)
  552. {
  553. icache_enable();
  554. dcache_enable();
  555. }