mmu.c 4.9 KB

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