mmu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <sys/types.h>
  25. #include <compiler.h>
  26. #include <arch.h>
  27. #include <arch/arm.h>
  28. #include <arch/arm/mmu.h>
  29. #include <platform/mt_reg_base.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <arch/ops.h>
  33. #include <lib/heap.h>
  34. #include <err.h>
  35. #if ARM_WITH_MMU
  36. #ifdef MTK_3LEVEL_PAGETABLE
  37. ld_tt_l2_info_t ld_tt_l2_info;
  38. uint64_t ld_tt_l1[4] __ALIGNED(32);
  39. /* convert user level mmu flags to flags that go in L1 descriptors */
  40. static uint64_t mmu_flags_to_l1_arch_flags(uint flags)
  41. {
  42. uint64_t arch_flags = 0;
  43. switch (flags & MMU_MEMORY_TYPE_MASK) {
  44. case MMU_MEMORY_TYPE_STRONGLY_ORDERED:
  45. arch_flags |= MMU_MEMORY_TYPE_STRONGLY_ORDERED;
  46. /* strongly order & device memory should be marked as XN */
  47. arch_flags |= MMU_L1_MEMORY_XN;
  48. break;
  49. case MMU_MEMORY_TYPE_NORMAL:
  50. arch_flags |= MMU_MEMORY_TYPE_NORMAL;
  51. break;
  52. case MMU_MEMORY_TYPE_NORMAL_WRITE_THROUGH:
  53. arch_flags |= MMU_MEMORY_TYPE_NORMAL_WRITE_THROUGH;
  54. break;
  55. case MMU_MEMORY_TYPE_DEVICE:
  56. arch_flags |= MMU_MEMORY_TYPE_DEVICE;
  57. /* strongly order & device memory should be marked as XN */
  58. arch_flags |= MMU_L1_MEMORY_XN;
  59. break;
  60. case MMU_MEMORY_TYPE_NORMAL_WRITE_BACK:
  61. arch_flags |= MMU_MEMORY_TYPE_NORMAL_WRITE_BACK;
  62. break;
  63. }
  64. switch (flags & MMU_MEMORY_AP_MASK) {
  65. case MMU_MEMORY_AP_P_RW_U_NA:
  66. arch_flags |= MMU_MEMORY_AP_P_RW_U_NA;
  67. break;
  68. case MMU_MEMORY_AP_P_RW_U_RW:
  69. arch_flags |= MMU_MEMORY_AP_P_RW_U_RW;
  70. break;
  71. case MMU_MEMORY_AP_P_R_U_NA:
  72. arch_flags |= MMU_MEMORY_AP_P_R_U_NA;
  73. break;
  74. case MMU_MEMORY_AP_P_R_U_R:
  75. arch_flags |= MMU_MEMORY_AP_P_R_U_R;
  76. break;
  77. }
  78. if (flags & MMU_MEMORY_ATTRIBUTE_XN) {
  79. arch_flags |= MMU_L1_MEMORY_XN;
  80. }
  81. arch_flags |= MMU_MEMORY_L1_AF;
  82. return arch_flags;
  83. }
  84. /* todo: table walk soultion */
  85. uint64_t arm_mmu_va2pa(unsigned int vaddr)
  86. {
  87. uint64_t paddr;
  88. /* trick, only work when paddr < 4G*/
  89. paddr = (uint64_t)vaddr;
  90. return paddr;
  91. }
  92. void mmu_update_tt_entry(uint64_t* tt_entry, uint64_t value)
  93. {
  94. /* Get the index into the translation table */
  95. *tt_entry = value;
  96. //arch_clean_invalidate_cache_range(tt_entry, CACHE_LINE);
  97. arch_clean_cache_range((addr_t)tt_entry, CACHE_LINE);
  98. }
  99. static status_t get_l2_table(uint32_t l1_index, uint64_t *ppa)
  100. {
  101. uint64_t pa;
  102. uint32_t *l2_va = NULL;
  103. DEBUG_ASSERT(ppa);
  104. /* allocate from static pool before heap init */
  105. if (ld_tt_l2_info.heap_init_done == 0) {
  106. if (ld_tt_l2_info.index < LD_TT_L2_STATIC_SIZE)
  107. l2_va = (void *)&ld_tt_l2_info.ld_tt_l2[ld_tt_l2_info.index++][0];
  108. else
  109. dprintf(CRITICAL, "mmu static pool too small!\n");
  110. } else {
  111. l2_va = heap_alloc(PAGE_SIZE, PAGE_SIZE); //malloc(PAGE_SIZE);
  112. }
  113. if (!l2_va)
  114. return ERR_NO_MEMORY;
  115. /* wipe it clean to set no access */
  116. memset(l2_va, 0, PAGE_SIZE);
  117. /* get physical address */
  118. pa = arm_mmu_va2pa((uint32_t)l2_va);
  119. *ppa = pa;
  120. return NO_ERROR;
  121. }
  122. int arch_mmu_map(uint64_t paddr, vaddr_t vaddr, uint flags, uint count)
  123. {
  124. int ret = NO_ERROR;
  125. /* paddr and vaddr must be aligned */
  126. DEBUG_ASSERT(IS_PAGE_ALIGNED(vaddr));
  127. DEBUG_ASSERT(IS_PAGE_ALIGNED(paddr));
  128. DEBUG_ASSERT(IS_PAGE_ALIGNED(count));
  129. if (!IS_PAGE_ALIGNED(vaddr) || !IS_PAGE_ALIGNED(paddr) || !IS_PAGE_ALIGNED(count))
  130. return ERR_INVALID_ARGS;
  131. if (count == 0)
  132. return NO_ERROR;
  133. while (count > 0) {
  134. if (IS_BLOCK_ALIGNED(vaddr) && IS_BLOCK_ALIGNED(paddr) && count >= BLOCK_SIZE) {
  135. /* can use a block, overwrite it! */
  136. uint l1_index = vaddr / BLOCK_SIZE;
  137. /* compute the arch flags for L1 sections */
  138. uint64_t arch_flags = mmu_flags_to_l1_arch_flags(flags) |
  139. MMU_MEMORY_L1_DESCRIPTOR_BLOCK;
  140. /* map it , 1GB */
  141. mmu_update_tt_entry(&ld_tt_l1[l1_index], paddr | arch_flags);
  142. count -= BLOCK_SIZE;
  143. vaddr += BLOCK_SIZE;
  144. paddr += BLOCK_SIZE;
  145. } else if (IS_SECTION_ALIGNED(vaddr) && IS_SECTION_ALIGNED(paddr) && count >= SECTION_SIZE) {
  146. uint l1_index = vaddr / BLOCK_SIZE;
  147. uint64_t tt_entry = ld_tt_l1[l1_index];
  148. switch (tt_entry & MMU_MEMORY_L1_DESCRIPTOR_MASK) {
  149. case MMU_MEMORY_L1_DESCRIPTOR_BLOCK:
  150. case MMU_MEMORY_L1_DESCRIPTOR_INVALID: {
  151. uint64_t l2_pa = 0;
  152. if ((ret = get_l2_table(l1_index, &l2_pa)) != NO_ERROR) {
  153. dprintf(CRITICAL, "failed to allocate l2 pagetable\n");
  154. goto _done;
  155. }
  156. tt_entry = l2_pa | MMU_MEMORY_L1_DESCRIPTOR_TABLE;
  157. mmu_update_tt_entry(&ld_tt_l1[l1_index], tt_entry);
  158. /* fallthrough */
  159. }
  160. case MMU_MEMORY_L1_DESCRIPTOR_TABLE: {
  161. uint64_t* l2_table = (uint64_t *)(uint32_t)(tt_entry & 0xfffff000);
  162. DEBUG_ASSERT(l2_table);
  163. /* compute the arch flags for L2 2MB */
  164. uint arch_flags = mmu_flags_to_l1_arch_flags(flags)| MMU_MEMORY_L1_DESCRIPTOR_BLOCK;;
  165. uint l2_index = (vaddr / SECTION_SIZE) & 0x1ff;
  166. mmu_update_tt_entry(&l2_table[l2_index], paddr | arch_flags);
  167. count -= SECTION_SIZE;
  168. vaddr += SECTION_SIZE;
  169. paddr += SECTION_SIZE;
  170. break;
  171. }
  172. default:
  173. PANIC_UNIMPLEMENTED;
  174. }
  175. } else if (IS_PAGE_ALIGNED(vaddr) && IS_PAGE_ALIGNED(paddr) && count >= PAGE_SIZE) {
  176. uint l1_index = vaddr / BLOCK_SIZE;
  177. uint64_t tt_entry = ld_tt_l1[l1_index];
  178. switch (tt_entry & MMU_MEMORY_L1_DESCRIPTOR_MASK) {
  179. case MMU_MEMORY_L1_DESCRIPTOR_BLOCK:
  180. case MMU_MEMORY_L1_DESCRIPTOR_INVALID: {
  181. uint64_t l2_pa = 0;
  182. if ((ret = get_l2_table(l1_index, &l2_pa)) != NO_ERROR) {
  183. dprintf(CRITICAL, "failed to allocate l2 pagetable\n");
  184. goto _done;
  185. }
  186. tt_entry = l2_pa | MMU_MEMORY_L1_DESCRIPTOR_TABLE;
  187. mmu_update_tt_entry(&ld_tt_l1[l1_index], tt_entry);
  188. /* fallthrough */
  189. }
  190. case MMU_MEMORY_L1_DESCRIPTOR_TABLE: {
  191. uint64_t* l2_table = (uint64_t *)(uint32_t)(tt_entry & 0xfffff000);
  192. DEBUG_ASSERT(l2_table);
  193. uint l2_index = (vaddr / SECTION_SIZE) & 0x1ff;
  194. uint64_t tt_entry = l2_table[l2_index];
  195. switch (tt_entry & MMU_MEMORY_L1_DESCRIPTOR_MASK) {
  196. case MMU_MEMORY_L1_DESCRIPTOR_BLOCK:
  197. case MMU_MEMORY_L1_DESCRIPTOR_INVALID: {
  198. uint64_t l3_pa = 0;
  199. if ((ret = get_l2_table(l1_index, &l3_pa)) != NO_ERROR) {
  200. dprintf(CRITICAL, "failed to allocate l3 pagetable\n");
  201. goto _done;
  202. }
  203. tt_entry = l3_pa | MMU_MEMORY_L1_DESCRIPTOR_TABLE;
  204. mmu_update_tt_entry(&l2_table[l2_index], tt_entry);
  205. /* fallthrough */
  206. }
  207. case MMU_MEMORY_L1_DESCRIPTOR_TABLE: {
  208. uint64_t* l3_table = (uint64_t *)(uint32_t)(tt_entry & 0xfffff000);
  209. DEBUG_ASSERT(l3_table);
  210. /* compute the arch flags for L3 4KB */
  211. uint arch_flags = mmu_flags_to_l1_arch_flags(flags)| MMU_MEMORY_L1_DESCRIPTOR_TABLE;;
  212. uint l3_index = (vaddr / PAGE_SIZE) & 0x1ff;
  213. mmu_update_tt_entry(&l3_table[l3_index], paddr | arch_flags);
  214. count -= PAGE_SIZE;
  215. vaddr += PAGE_SIZE;
  216. paddr += PAGE_SIZE;
  217. break;
  218. }
  219. default:
  220. PANIC_UNIMPLEMENTED;
  221. }
  222. break;
  223. }
  224. default:
  225. PANIC_UNIMPLEMENTED;
  226. }
  227. } else
  228. DEBUG_ASSERT(0);
  229. }
  230. _done:
  231. arm_invalidate_tlb();
  232. DSB;
  233. ISB;
  234. return ret;
  235. }
  236. void arm_mmu_lpae_init(void)
  237. {
  238. unsigned int ttbcr = (SREG_TTBCR_EAE | SREG_TTBCR_SH1 | SREG_TTBCR_SH0);
  239. unsigned int mair0 = MMU_MAIR0;
  240. unsigned int mair1 = MMU_MAIR1;
  241. /* set some mmu specific control bits:
  242. * access flag disabled, TEX remap disabled, mmu disabled
  243. */
  244. arm_write_cr1(arm_read_cr1() & ~(SREG_SCTLR_AFE|SREG_SCTLR_TRE|SREG_SCTLR_M));
  245. __asm__ volatile("mcr p15, 0, %0, c2, c0, 2" :: "r" (ttbcr));
  246. __asm__ volatile("mcr p15, 0, %0, c10, c2, 0" :: "r" (mair0));
  247. __asm__ volatile("mcr p15, 0, %0, c10, c2, 1" :: "r" (mair1));
  248. /* set up the translation table base */
  249. arm_write_ttbr((uint32_t)ld_tt_l1);
  250. /* set up the domain access register */
  251. arm_write_dacr(0x00000001);
  252. }
  253. void arch_enable_mmu(void)
  254. {
  255. arm_write_cr1(arm_read_cr1() | SREG_SCTLR_M);
  256. }
  257. void arch_disable_mmu(void)
  258. {
  259. arm_write_cr1(arm_read_cr1() & ~SREG_SCTLR_M);
  260. }
  261. #else //!MTK_3LEVEL_PAGETABLE
  262. #define MB (1024*1024)
  263. #define GB (1024*1024*1024)
  264. /* the location of the table may be brought in from outside */
  265. #if WITH_EXTERNAL_TRANSLATION_TABLE
  266. #if !defined(MMU_TRANSLATION_TABLE_ADDR)
  267. #error must set MMU_TRANSLATION_TABLE_ADDR in the make configuration
  268. #endif
  269. static uint32_t *tt = (void *)MMU_TRANSLATION_TABLE_ADDR;
  270. #else
  271. /* the main translation table */
  272. static uint32_t tt[4096] __ALIGNED(16384);
  273. #endif
  274. #ifndef MTK_LM_2LEVEL_PAGETABLE_MODE
  275. static uint64_t *lpae_tt = (uint64_t *)tt;
  276. #else
  277. /* L1 must align it's table size */
  278. static uint64_t ld_tt_l1[4] __ALIGNED(32);
  279. static uint64_t *ld_tt_l2 = (uint64_t *)tt;
  280. #endif
  281. void arm_mmu_map_section(addr_t paddr, addr_t vaddr, uint flags)
  282. {
  283. int index;
  284. /* Get the index into the translation table */
  285. index = vaddr / MB;
  286. /* Set the entry value:
  287. * (2<<0): Section entry
  288. * (0<<5): Domain = 0
  289. * flags: TEX, CB and AP bit settings provided by the caller.
  290. */
  291. tt[index] = (paddr & ~(MB-1)) | (0<<5) | (2<<0) | flags;
  292. arm_invalidate_tlb();
  293. }
  294. void arm_mmu_map_block(unsigned long long paddr, addr_t vaddr, unsigned long long flags)
  295. {
  296. /* Get the index into the translation table */
  297. #ifndef MTK_LM_2LEVEL_PAGETABLE_MODE
  298. int index = vaddr / GB;
  299. lpae_tt[index] = (paddr & (0x000000FFC0000000ULL)) | (0x1<<10) | (0x3<<8) | (0x1<<0) | flags;
  300. #else
  301. int index = vaddr / (2*MB);
  302. ld_tt_l2[index] = (paddr & (0x000000FFFFE00000ULL)) | (0x1<<10) | (0x3<<8) | (0x1<<0) | flags;
  303. #endif
  304. arm_invalidate_tlb();
  305. }
  306. unsigned long long arm_mmu_va2pa(unsigned int vaddr)
  307. {
  308. unsigned long long paddr;
  309. int index;
  310. unsigned int ttbcr;
  311. __asm__ volatile("mrc p15, 0, %0, c2, c0, 2" : "=r" (ttbcr));
  312. if (!(ttbcr & (0x1 << 31))) {
  313. index = vaddr / MB;
  314. paddr = (tt[index] & ~(MB-1)) | (vaddr & (MB-1));
  315. } else {
  316. #ifndef MTK_LM_2LEVEL_PAGETABLE_MODE
  317. index = vaddr / GB;
  318. paddr = (lpae_tt[index] & (0x000000FFC0000000ULL)) | (vaddr & (GB-1));
  319. #else
  320. index = vaddr / (2*MB);
  321. paddr = (ld_tt_l2[index] & (0x000000FFFFE00000ULL)) | (vaddr & ((2*MB)-1));
  322. #endif
  323. }
  324. return paddr;
  325. }
  326. void arm_mmu_init(void)
  327. {
  328. unsigned int i;
  329. //extern u64 physical_memory_size(void)__attribute__((weak));
  330. extern uint64_t physical_memory_size(void)__attribute__((weak));
  331. /* set some mmu specific control bits:
  332. * access flag disabled, TEX remap disabled, mmu disabled
  333. */
  334. arm_write_cr1(arm_read_cr1() & ~((1<<29)|(1<<28)|(1<<0)));
  335. if (physical_memory_size) {
  336. unsigned int dram_size = 0;
  337. unsigned int mapping = 0;
  338. uint64_t total_size = 0;
  339. dram_size = physical_memory_size();
  340. total_size = (uint64_t)DRAM_PHY_ADDR + (uint64_t)dram_size;
  341. mapping = total_size/(MB);
  342. if (mapping >= 4096)
  343. mapping = 4096;
  344. /* set up an identity-mapped translation table with
  345. * strongly ordered memory type and read/write access.
  346. */
  347. for (i=0; i < mapping; i++) {
  348. arm_mmu_map_section(i * MB,
  349. i * MB,
  350. MMU_MEMORY_TYPE_STRONGLY_ORDERED |
  351. MMU_MEMORY_AP_READ_WRITE);
  352. }
  353. }
  354. else {
  355. /* set up an identity-mapped translation table with
  356. * strongly ordered memory type and read/write access.
  357. */
  358. for (i=0; i < 4096; i++) {
  359. arm_mmu_map_section(i * MB,
  360. i * MB,
  361. MMU_MEMORY_TYPE_STRONGLY_ORDERED |
  362. MMU_MEMORY_AP_READ_WRITE);
  363. }
  364. }
  365. /* set up the translation table base */
  366. arm_write_ttbr((uint32_t)tt);
  367. /* set up the domain access register */
  368. arm_write_dacr(0x00000001);
  369. }
  370. void arm_mmu_lpae_init(void)
  371. {
  372. unsigned int i;
  373. unsigned int ttbcr = 0xB0003000;
  374. unsigned int mair0 = 0xeeaa4400;
  375. unsigned int mair1 = 0xff000004;
  376. /* set some mmu specific control bits:
  377. * access flag disabled, TEX remap disabled, mmu disabled
  378. */
  379. arm_write_cr1(arm_read_cr1() & ~((1<<29)|(1<<28)|(1<<0)));
  380. __asm__ volatile("mcr p15, 0, %0, c2, c0, 2" :: "r" (ttbcr));
  381. __asm__ volatile("mcr p15, 0, %0, c10, c2, 0" :: "r" (mair0));
  382. __asm__ volatile("mcr p15, 0, %0, c10, c2, 1" :: "r" (mair1));
  383. #ifndef MTK_LM_2LEVEL_PAGETABLE_MODE
  384. /* set up an identity-mapped translation table with
  385. * strongly ordered memory type and read/write access.
  386. */
  387. for (i=0; i < 4; i++) {
  388. arm_mmu_map_block(i * GB, i * GB, LPAE_MMU_MEMORY_TYPE_STRONGLY_ORDERED);
  389. }
  390. /* set up the translation table base */
  391. arm_write_ttbr((uint32_t)lpae_tt);
  392. #else
  393. /* setup L1 table */
  394. for (i=0; i < 4; i++) {
  395. ld_tt_l1[i] = ((uint64_t)((uint32_t)&ld_tt_l2[512*i])) | (0x3<<0);
  396. }
  397. /* l2 table mapping */
  398. for (i=0; i < 2048; i++) {
  399. ld_tt_l2[i] = i*(2*MB) | (0x1<<10) | (0x3<<8)| (0x1<<0) | LPAE_MMU_MEMORY_TYPE_STRONGLY_ORDERED;
  400. }
  401. /* set up the translation table base */
  402. arm_write_ttbr((uint32_t)ld_tt_l1);
  403. #endif
  404. /* set up the domain access register */
  405. arm_write_dacr(0x00000001);
  406. /* turn on the mmu */
  407. //arm_write_cr1(arm_read_cr1() | 0x1);
  408. }
  409. void arch_enable_mmu(void)
  410. {
  411. arm_write_cr1(arm_read_cr1() | 0x1);
  412. }
  413. void arch_disable_mmu(void)
  414. {
  415. arm_write_cr1(arm_read_cr1() & ~(1<<0));
  416. }
  417. #endif //MTK_3LEVEL_PAGETABLE
  418. #endif // ARM_WITH_MMU