cache_v8.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. static 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].base + 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. /* Add one mm_region map entry to the page tables */
  143. static void add_map(struct mm_region *map)
  144. {
  145. u64 *pte;
  146. u64 addr = map->base;
  147. u64 size = map->size;
  148. u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
  149. u64 blocksize;
  150. int level;
  151. u64 *new_table;
  152. while (size) {
  153. pte = find_pte(addr, 0);
  154. if (pte && (pte_type(pte) == PTE_TYPE_FAULT)) {
  155. debug("Creating table for addr 0x%llx\n", addr);
  156. new_table = create_table();
  157. set_pte_table(pte, new_table);
  158. }
  159. for (level = 1; level < 4; level++) {
  160. pte = find_pte(addr, level);
  161. blocksize = 1ULL << level2shift(level);
  162. debug("Checking if pte fits for addr=%llx size=%llx "
  163. "blocksize=%llx\n", addr, size, blocksize);
  164. if (size >= blocksize && !(addr & (blocksize - 1))) {
  165. /* Page fits, create block PTE */
  166. debug("Setting PTE %p to block addr=%llx\n",
  167. pte, addr);
  168. *pte = addr | attrs;
  169. addr += blocksize;
  170. size -= blocksize;
  171. break;
  172. } else if ((pte_type(pte) == PTE_TYPE_FAULT)) {
  173. /* Page doesn't fit, create subpages */
  174. debug("Creating subtable for addr 0x%llx "
  175. "blksize=%llx\n", addr, blocksize);
  176. new_table = create_table();
  177. set_pte_table(pte, new_table);
  178. }
  179. }
  180. }
  181. }
  182. /* Splits a block PTE into table with subpages spanning the old block */
  183. static void split_block(u64 *pte, int level)
  184. {
  185. u64 old_pte = *pte;
  186. u64 *new_table;
  187. u64 i = 0;
  188. /* level describes the parent level, we need the child ones */
  189. int levelshift = level2shift(level + 1);
  190. if (pte_type(pte) != PTE_TYPE_BLOCK)
  191. panic("PTE %p (%llx) is not a block. Some driver code wants to "
  192. "modify dcache settings for an range not covered in "
  193. "mem_map.", pte, old_pte);
  194. new_table = create_table();
  195. debug("Splitting pte %p (%llx) into %p\n", pte, old_pte, new_table);
  196. for (i = 0; i < MAX_PTE_ENTRIES; i++) {
  197. new_table[i] = old_pte | (i << levelshift);
  198. /* Level 3 block PTEs have the table type */
  199. if ((level + 1) == 3)
  200. new_table[i] |= PTE_TYPE_TABLE;
  201. debug("Setting new_table[%lld] = %llx\n", i, new_table[i]);
  202. }
  203. /* Set the new table into effect */
  204. set_pte_table(pte, new_table);
  205. }
  206. enum pte_type {
  207. PTE_INVAL,
  208. PTE_BLOCK,
  209. PTE_LEVEL,
  210. };
  211. /*
  212. * This is a recursively called function to count the number of
  213. * page tables we need to cover a particular PTE range. If you
  214. * call this with level = -1 you basically get the full 48 bit
  215. * coverage.
  216. */
  217. static int count_required_pts(u64 addr, int level, u64 maxaddr)
  218. {
  219. int levelshift = level2shift(level);
  220. u64 levelsize = 1ULL << levelshift;
  221. u64 levelmask = levelsize - 1;
  222. u64 levelend = addr + levelsize;
  223. int r = 0;
  224. int i;
  225. enum pte_type pte_type = PTE_INVAL;
  226. for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) {
  227. struct mm_region *map = &mem_map[i];
  228. u64 start = map->base;
  229. u64 end = start + map->size;
  230. /* Check if the PTE would overlap with the map */
  231. if (max(addr, start) <= min(levelend, end)) {
  232. start = max(addr, start);
  233. end = min(levelend, end);
  234. /* We need a sub-pt for this level */
  235. if ((start & levelmask) || (end & levelmask)) {
  236. pte_type = PTE_LEVEL;
  237. break;
  238. }
  239. /* Lv0 can not do block PTEs, so do levels here too */
  240. if (level <= 0) {
  241. pte_type = PTE_LEVEL;
  242. break;
  243. }
  244. /* PTE is active, but fits into a block */
  245. pte_type = PTE_BLOCK;
  246. }
  247. }
  248. /*
  249. * Block PTEs at this level are already covered by the parent page
  250. * table, so we only need to count sub page tables.
  251. */
  252. if (pte_type == PTE_LEVEL) {
  253. int sublevel = level + 1;
  254. u64 sublevelsize = 1ULL << level2shift(sublevel);
  255. /* Account for the new sub page table ... */
  256. r = 1;
  257. /* ... and for all child page tables that one might have */
  258. for (i = 0; i < MAX_PTE_ENTRIES; i++) {
  259. r += count_required_pts(addr, sublevel, maxaddr);
  260. addr += sublevelsize;
  261. if (addr >= maxaddr) {
  262. /*
  263. * We reached the end of address space, no need
  264. * to look any further.
  265. */
  266. break;
  267. }
  268. }
  269. }
  270. return r;
  271. }
  272. /* Returns the estimated required size of all page tables */
  273. u64 get_page_table_size(void)
  274. {
  275. u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
  276. u64 size = 0;
  277. u64 va_bits;
  278. int start_level = 0;
  279. get_tcr(0, NULL, &va_bits);
  280. if (va_bits < 39)
  281. start_level = 1;
  282. /* Account for all page tables we would need to cover our memory map */
  283. size = one_pt * count_required_pts(0, start_level - 1, 1ULL << va_bits);
  284. /*
  285. * We need to duplicate our page table once to have an emergency pt to
  286. * resort to when splitting page tables later on
  287. */
  288. size *= 2;
  289. /*
  290. * We may need to split page tables later on if dcache settings change,
  291. * so reserve up to 4 (random pick) page tables for that.
  292. */
  293. size += one_pt * 4;
  294. return size;
  295. }
  296. static void setup_pgtables(void)
  297. {
  298. int i;
  299. /*
  300. * Allocate the first level we're on with invalidate entries.
  301. * If the starting level is 0 (va_bits >= 39), then this is our
  302. * Lv0 page table, otherwise it's the entry Lv1 page table.
  303. */
  304. create_table();
  305. /* Now add all MMU table entries one after another to the table */
  306. for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
  307. add_map(&mem_map[i]);
  308. /* Create the same thing once more for our emergency page table */
  309. create_table();
  310. }
  311. static void setup_all_pgtables(void)
  312. {
  313. u64 tlb_addr = gd->arch.tlb_addr;
  314. /* Reset the fill ptr */
  315. gd->arch.tlb_fillptr = tlb_addr;
  316. /* Create normal system page tables */
  317. setup_pgtables();
  318. /* Create emergency page tables */
  319. gd->arch.tlb_addr = gd->arch.tlb_fillptr;
  320. setup_pgtables();
  321. gd->arch.tlb_emerg = gd->arch.tlb_addr;
  322. gd->arch.tlb_addr = tlb_addr;
  323. }
  324. /* to activate the MMU we need to set up virtual memory */
  325. __weak void mmu_setup(void)
  326. {
  327. int el;
  328. /* Set up page tables only once */
  329. if (!gd->arch.tlb_fillptr)
  330. setup_all_pgtables();
  331. el = current_el();
  332. set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
  333. MEMORY_ATTRIBUTES);
  334. /* enable the mmu */
  335. set_sctlr(get_sctlr() | CR_M);
  336. }
  337. /*
  338. * Performs a invalidation of the entire data cache at all levels
  339. */
  340. void invalidate_dcache_all(void)
  341. {
  342. __asm_invalidate_dcache_all();
  343. }
  344. /*
  345. * Performs a clean & invalidation of the entire data cache at all levels.
  346. * This function needs to be inline to avoid using stack.
  347. * __asm_flush_l3_cache return status of timeout
  348. */
  349. inline void flush_dcache_all(void)
  350. {
  351. int ret;
  352. __asm_flush_dcache_all();
  353. ret = __asm_flush_l3_cache();
  354. if (ret)
  355. debug("flushing dcache returns 0x%x\n", ret);
  356. else
  357. debug("flushing dcache successfully.\n");
  358. }
  359. /*
  360. * Invalidates range in all levels of D-cache/unified cache
  361. */
  362. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  363. {
  364. __asm_flush_dcache_range(start, stop);
  365. }
  366. /*
  367. * Flush range(clean & invalidate) from all levels of D-cache/unified cache
  368. */
  369. void flush_dcache_range(unsigned long start, unsigned long stop)
  370. {
  371. __asm_flush_dcache_range(start, stop);
  372. }
  373. void dcache_enable(void)
  374. {
  375. /* The data cache is not active unless the mmu is enabled */
  376. if (!(get_sctlr() & CR_M)) {
  377. invalidate_dcache_all();
  378. __asm_invalidate_tlb_all();
  379. mmu_setup();
  380. }
  381. set_sctlr(get_sctlr() | CR_C);
  382. }
  383. void dcache_disable(void)
  384. {
  385. uint32_t sctlr;
  386. sctlr = get_sctlr();
  387. /* if cache isn't enabled no need to disable */
  388. if (!(sctlr & CR_C))
  389. return;
  390. set_sctlr(sctlr & ~(CR_C|CR_M));
  391. flush_dcache_all();
  392. __asm_invalidate_tlb_all();
  393. }
  394. int dcache_status(void)
  395. {
  396. return (get_sctlr() & CR_C) != 0;
  397. }
  398. u64 *__weak arch_get_page_table(void) {
  399. puts("No page table offset defined\n");
  400. return NULL;
  401. }
  402. static bool is_aligned(u64 addr, u64 size, u64 align)
  403. {
  404. return !(addr & (align - 1)) && !(size & (align - 1));
  405. }
  406. static u64 set_one_region(u64 start, u64 size, u64 attrs, int level)
  407. {
  408. int levelshift = level2shift(level);
  409. u64 levelsize = 1ULL << levelshift;
  410. u64 *pte = find_pte(start, level);
  411. /* Can we can just modify the current level block PTE? */
  412. if (is_aligned(start, size, levelsize)) {
  413. *pte &= ~PMD_ATTRINDX_MASK;
  414. *pte |= attrs;
  415. debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
  416. return levelsize;
  417. }
  418. /* Unaligned or doesn't fit, maybe split block into table */
  419. debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
  420. /* Maybe we need to split the block into a table */
  421. if (pte_type(pte) == PTE_TYPE_BLOCK)
  422. split_block(pte, level);
  423. /* And then double-check it became a table or already is one */
  424. if (pte_type(pte) != PTE_TYPE_TABLE)
  425. panic("PTE %p (%llx) for addr=%llx should be a table",
  426. pte, *pte, start);
  427. /* Roll on to the next page table level */
  428. return 0;
  429. }
  430. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  431. enum dcache_option option)
  432. {
  433. u64 attrs = PMD_ATTRINDX(option);
  434. u64 real_start = start;
  435. u64 real_size = size;
  436. debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
  437. /*
  438. * We can not modify page tables that we're currently running on,
  439. * so we first need to switch to the "emergency" page tables where
  440. * we can safely modify our primary page tables and then switch back
  441. */
  442. __asm_switch_ttbr(gd->arch.tlb_emerg);
  443. /*
  444. * Loop through the address range until we find a page granule that fits
  445. * our alignment constraints, then set it to the new cache attributes
  446. */
  447. while (size > 0) {
  448. int level;
  449. u64 r;
  450. for (level = 1; level < 4; level++) {
  451. r = set_one_region(start, size, attrs, level);
  452. if (r) {
  453. /* PTE successfully replaced */
  454. size -= r;
  455. start += r;
  456. break;
  457. }
  458. }
  459. }
  460. /* We're done modifying page tables, switch back to our primary ones */
  461. __asm_switch_ttbr(gd->arch.tlb_addr);
  462. /*
  463. * Make sure there's nothing stale in dcache for a region that might
  464. * have caches off now
  465. */
  466. flush_dcache_range(real_start, real_start + real_size);
  467. }
  468. #else /* CONFIG_SYS_DCACHE_OFF */
  469. void invalidate_dcache_all(void)
  470. {
  471. }
  472. void flush_dcache_all(void)
  473. {
  474. }
  475. void dcache_enable(void)
  476. {
  477. }
  478. void dcache_disable(void)
  479. {
  480. }
  481. int dcache_status(void)
  482. {
  483. return 0;
  484. }
  485. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  486. enum dcache_option option)
  487. {
  488. }
  489. #endif /* CONFIG_SYS_DCACHE_OFF */
  490. #ifndef CONFIG_SYS_ICACHE_OFF
  491. void icache_enable(void)
  492. {
  493. __asm_invalidate_icache_all();
  494. set_sctlr(get_sctlr() | CR_I);
  495. }
  496. void icache_disable(void)
  497. {
  498. set_sctlr(get_sctlr() & ~CR_I);
  499. }
  500. int icache_status(void)
  501. {
  502. return (get_sctlr() & CR_I) != 0;
  503. }
  504. void invalidate_icache_all(void)
  505. {
  506. __asm_invalidate_icache_all();
  507. }
  508. #else /* CONFIG_SYS_ICACHE_OFF */
  509. void icache_enable(void)
  510. {
  511. }
  512. void icache_disable(void)
  513. {
  514. }
  515. int icache_status(void)
  516. {
  517. return 0;
  518. }
  519. void invalidate_icache_all(void)
  520. {
  521. }
  522. #endif /* CONFIG_SYS_ICACHE_OFF */
  523. /*
  524. * Enable dCache & iCache, whether cache is actually enabled
  525. * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
  526. */
  527. void __weak enable_caches(void)
  528. {
  529. icache_enable();
  530. dcache_enable();
  531. }