mmu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-01-10 bernard porting to AM1808
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <board.h>
  13. #include "cp15.h"
  14. #include "mmu.h"
  15. /* dump 2nd level page table */
  16. void rt_hw_cpu_dump_page_table_2nd(rt_uint32_t *ptb)
  17. {
  18. int i;
  19. int fcnt = 0;
  20. for (i = 0; i < 256; i++)
  21. {
  22. rt_uint32_t pte2 = ptb[i];
  23. if ((pte2 & 0x3) == 0)
  24. {
  25. if (fcnt == 0)
  26. rt_kprintf(" ");
  27. rt_kprintf("%04x: ", i);
  28. fcnt++;
  29. if (fcnt == 16)
  30. {
  31. rt_kprintf("fault\n");
  32. fcnt = 0;
  33. }
  34. continue;
  35. }
  36. if (fcnt != 0)
  37. {
  38. rt_kprintf("fault\n");
  39. fcnt = 0;
  40. }
  41. rt_kprintf(" %04x: %x: ", i, pte2);
  42. if ((pte2 & 0x3) == 0x1)
  43. {
  44. rt_kprintf("L,ap:%x,xn:%d,texcb:%02x\n",
  45. ((pte2 >> 7) | (pte2 >> 4))& 0xf,
  46. (pte2 >> 15) & 0x1,
  47. ((pte2 >> 10) | (pte2 >> 2)) & 0x1f);
  48. }
  49. else
  50. {
  51. rt_kprintf("S,ap:%x,xn:%d,texcb:%02x\n",
  52. ((pte2 >> 7) | (pte2 >> 4))& 0xf, pte2 & 0x1,
  53. ((pte2 >> 4) | (pte2 >> 2)) & 0x1f);
  54. }
  55. }
  56. }
  57. void rt_hw_cpu_dump_page_table(rt_uint32_t *ptb)
  58. {
  59. int i;
  60. int fcnt = 0;
  61. rt_kprintf("page table@%p\n", ptb);
  62. for (i = 0; i < 1024*4; i++)
  63. {
  64. rt_uint32_t pte1 = ptb[i];
  65. if ((pte1 & 0x3) == 0)
  66. {
  67. rt_kprintf("%03x: ", i);
  68. fcnt++;
  69. if (fcnt == 16)
  70. {
  71. rt_kprintf("fault\n");
  72. fcnt = 0;
  73. }
  74. continue;
  75. }
  76. if (fcnt != 0)
  77. {
  78. rt_kprintf("fault\n");
  79. fcnt = 0;
  80. }
  81. rt_kprintf("%03x: %08x: ", i, pte1);
  82. if ((pte1 & 0x3) == 0x3)
  83. {
  84. rt_kprintf("LPAE\n");
  85. }
  86. else if ((pte1 & 0x3) == 0x1)
  87. {
  88. rt_kprintf("pte,ns:%d,domain:%d\n",
  89. (pte1 >> 3) & 0x1, (pte1 >> 5) & 0xf);
  90. /*
  91. *rt_hw_cpu_dump_page_table_2nd((void*)((pte1 & 0xfffffc000)
  92. * - 0x80000000 + 0xC0000000));
  93. */
  94. }
  95. else if (pte1 & (1 << 18))
  96. {
  97. rt_kprintf("super section,ns:%d,ap:%x,xn:%d,texcb:%02x\n",
  98. (pte1 >> 19) & 0x1,
  99. ((pte1 >> 13) | (pte1 >> 10))& 0xf,
  100. (pte1 >> 4) & 0x1,
  101. ((pte1 >> 10) | (pte1 >> 2)) & 0x1f);
  102. }
  103. else
  104. {
  105. rt_kprintf("section,ns:%d,ap:%x,"
  106. "xn:%d,texcb:%02x,domain:%d\n",
  107. (pte1 >> 19) & 0x1,
  108. ((pte1 >> 13) | (pte1 >> 10))& 0xf,
  109. (pte1 >> 4) & 0x1,
  110. (((pte1 & (0x7 << 12)) >> 10) |
  111. ((pte1 & 0x0c) >> 2)) & 0x1f,
  112. (pte1 >> 5) & 0xf);
  113. }
  114. }
  115. }
  116. /* level1 page table, each entry for 1MB memory. */
  117. volatile static unsigned long MMUTable[4*1024] __attribute__((aligned(16*1024)));
  118. void rt_hw_mmu_setmtt(rt_uint32_t vaddrStart,
  119. rt_uint32_t vaddrEnd,
  120. rt_uint32_t paddrStart,
  121. rt_uint32_t attr)
  122. {
  123. volatile rt_uint32_t *pTT;
  124. volatile int i, nSec;
  125. pTT = (rt_uint32_t *)MMUTable + (vaddrStart >> 20);
  126. nSec = (vaddrEnd >> 20) - (vaddrStart >> 20);
  127. for(i = 0; i <= nSec; i++)
  128. {
  129. *pTT = attr | (((paddrStart >> 20) + i) << 20);
  130. pTT++;
  131. }
  132. }
  133. unsigned long rt_hw_set_domain_register(unsigned long domain_val)
  134. {
  135. unsigned long old_domain;
  136. asm volatile ("mrc p15, 0, %0, c3, c0\n" : "=r" (old_domain));
  137. asm volatile ("mcr p15, 0, %0, c3, c0\n" : :"r" (domain_val) : "memory");
  138. return old_domain;
  139. }
  140. void rt_hw_init_mmu_table(struct mem_desc *mdesc, rt_uint32_t size)
  141. {
  142. /* set page table */
  143. for(; size > 0; size--)
  144. {
  145. rt_hw_mmu_setmtt(mdesc->vaddr_start, mdesc->vaddr_end,
  146. mdesc->paddr_start, mdesc->attr);
  147. mdesc++;
  148. }
  149. }
  150. void rt_hw_mmu_init(void)
  151. {
  152. rt_cpu_dcache_clean_flush();
  153. rt_cpu_icache_flush();
  154. rt_hw_cpu_dcache_disable();
  155. rt_hw_cpu_icache_disable();
  156. rt_cpu_mmu_disable();
  157. /*rt_hw_cpu_dump_page_table(MMUTable);*/
  158. rt_hw_set_domain_register(0x55555555);
  159. rt_cpu_tlb_set(MMUTable);
  160. rt_cpu_mmu_enable();
  161. rt_hw_cpu_icache_enable();
  162. rt_hw_cpu_dcache_enable();
  163. }