mmu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * File : mmu.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-01-10 bernard porting to AM1808
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <board.h>
  17. #include "cp15.h"
  18. #define DESC_SEC (0x2)
  19. #define CB (3<<2) //cache_on, write_back
  20. #define CNB (2<<2) //cache_on, write_through
  21. #define NCB (1<<2) //cache_off,WR_BUF on
  22. #define NCNB (0<<2) //cache_off,WR_BUF off
  23. #define AP_RW (3<<10) //supervisor=RW, user=RW
  24. #define AP_RO (2<<10) //supervisor=RW, user=RO
  25. #define XN (1<<4) // eXecute Never
  26. #define DOMAIN_FAULT (0x0)
  27. #define DOMAIN_CHK (0x1)
  28. #define DOMAIN_NOTCHK (0x3)
  29. #define DOMAIN0 (0x0<<5)
  30. #define DOMAIN1 (0x1<<5)
  31. #define DOMAIN0_ATTR (DOMAIN_CHK<<0)
  32. #define DOMAIN1_ATTR (DOMAIN_FAULT<<2)
  33. /* Read/Write, cache, write back */
  34. #define RW_CB (AP_RW|DOMAIN0|CB|DESC_SEC)
  35. /* Read/Write, cache, write through */
  36. #define RW_CNB (AP_RW|DOMAIN0|CNB|DESC_SEC)
  37. /* Read/Write without cache and write buffer */
  38. #define RW_NCNB (AP_RW|DOMAIN0|NCNB|DESC_SEC)
  39. /* Read/Write without cache and write buffer, no execute */
  40. #define RW_NCNBXN (AP_RW|DOMAIN0|NCNB|DESC_SEC|XN)
  41. /* Read/Write without cache and write buffer */
  42. #define RW_FAULT (AP_RW|DOMAIN1|NCNB|DESC_SEC)
  43. /* dump 2nd level page table */
  44. void rt_hw_cpu_dump_page_table_2nd(rt_uint32_t *ptb)
  45. {
  46. int i;
  47. int fcnt = 0;
  48. for (i = 0; i < 256; i++)
  49. {
  50. rt_uint32_t pte2 = ptb[i];
  51. if ((pte2 & 0x3) == 0)
  52. {
  53. if (fcnt == 0)
  54. rt_kprintf(" ");
  55. rt_kprintf("%04x: ", i);
  56. fcnt++;
  57. if (fcnt == 16)
  58. {
  59. rt_kprintf("fault\n");
  60. fcnt = 0;
  61. }
  62. continue;
  63. }
  64. if (fcnt != 0)
  65. {
  66. rt_kprintf("fault\n");
  67. fcnt = 0;
  68. }
  69. rt_kprintf(" %04x: %x: ", i, pte2);
  70. if ((pte2 & 0x3) == 0x1)
  71. {
  72. rt_kprintf("L,ap:%x,xn:%d,texcb:%02x\n",
  73. ((pte2 >> 7) | (pte2 >> 4))& 0xf,
  74. (pte2 >> 15) & 0x1,
  75. ((pte2 >> 10) | (pte2 >> 2)) & 0x1f);
  76. }
  77. else
  78. {
  79. rt_kprintf("S,ap:%x,xn:%d,texcb:%02x\n",
  80. ((pte2 >> 7) | (pte2 >> 4))& 0xf, pte2 & 0x1,
  81. ((pte2 >> 4) | (pte2 >> 2)) & 0x1f);
  82. }
  83. }
  84. }
  85. void rt_hw_cpu_dump_page_table(rt_uint32_t *ptb)
  86. {
  87. int i;
  88. int fcnt = 0;
  89. rt_kprintf("page table@%p\n", ptb);
  90. for (i = 0; i < 1024*4; i++)
  91. {
  92. rt_uint32_t pte1 = ptb[i];
  93. if ((pte1 & 0x3) == 0)
  94. {
  95. rt_kprintf("%03x: ", i);
  96. fcnt++;
  97. if (fcnt == 16)
  98. {
  99. rt_kprintf("fault\n");
  100. fcnt = 0;
  101. }
  102. continue;
  103. }
  104. if (fcnt != 0)
  105. {
  106. rt_kprintf("fault\n");
  107. fcnt = 0;
  108. }
  109. rt_kprintf("%03x: %08x: ", i, pte1);
  110. if ((pte1 & 0x3) == 0x3)
  111. {
  112. rt_kprintf("LPAE\n");
  113. }
  114. else if ((pte1 & 0x3) == 0x1)
  115. {
  116. rt_kprintf("pte,ns:%d,domain:%d\n",
  117. (pte1 >> 3) & 0x1, (pte1 >> 5) & 0xf);
  118. /*
  119. *rt_hw_cpu_dump_page_table_2nd((void*)((pte1 & 0xfffffc000)
  120. * - 0x80000000 + 0xC0000000));
  121. */
  122. }
  123. else if (pte1 & (1 << 18))
  124. {
  125. rt_kprintf("super section,ns:%d,ap:%x,xn:%d,texcb:%02x\n",
  126. (pte1 >> 19) & 0x1,
  127. ((pte1 >> 13) | (pte1 >> 10))& 0xf,
  128. (pte1 >> 4) & 0x1,
  129. ((pte1 >> 10) | (pte1 >> 2)) & 0x1f);
  130. }
  131. else
  132. {
  133. rt_kprintf("section,ns:%d,ap:%x,"
  134. "xn:%d,texcb:%02x,domain:%d\n",
  135. (pte1 >> 19) & 0x1,
  136. ((pte1 >> 13) | (pte1 >> 10))& 0xf,
  137. (pte1 >> 4) & 0x1,
  138. (((pte1 & (0x7 << 12)) >> 10) |
  139. ((pte1 & 0x0c) >> 2)) & 0x1f,
  140. (pte1 >> 5) & 0xf);
  141. }
  142. }
  143. }
  144. /* level1 page table, each entry for 1MB memory. */
  145. volatile static unsigned long MMUTable[4*1024] __attribute__((aligned(16*1024)));
  146. void rt_hw_mmu_setmtt(rt_uint32_t vaddrStart,
  147. rt_uint32_t vaddrEnd,
  148. rt_uint32_t paddrStart,
  149. rt_uint32_t attr)
  150. {
  151. volatile rt_uint32_t *pTT;
  152. volatile int i, nSec;
  153. pTT = (rt_uint32_t *)MMUTable + (vaddrStart >> 20);
  154. nSec = (vaddrEnd >> 20) - (vaddrStart >> 20);
  155. for(i = 0; i <= nSec; i++)
  156. {
  157. *pTT = attr | (((paddrStart >> 20) + i) << 20);
  158. pTT++;
  159. }
  160. }
  161. unsigned long rt_hw_set_domain_register(unsigned long domain_val)
  162. {
  163. unsigned long old_domain;
  164. asm volatile ("mrc p15, 0, %0, c3, c0\n" : "=r" (old_domain));
  165. asm volatile ("mcr p15, 0, %0, c3, c0\n" : :"r" (domain_val) : "memory");
  166. return old_domain;
  167. }
  168. void rt_hw_mmu_init(void)
  169. {
  170. rt_hw_cpu_dcache_disable();
  171. rt_hw_cpu_icache_disable();
  172. rt_cpu_mmu_disable();
  173. /* set page table */
  174. /* 4G 1:1 memory */
  175. rt_hw_mmu_setmtt(0, 0xffffffff-1, 0, RW_CB);
  176. /* IO memory region */
  177. rt_hw_mmu_setmtt(0x44000000, 0x80000000-1, 0x44000000, RW_NCNBXN);
  178. /*rt_hw_cpu_dump_page_table(MMUTable);*/
  179. rt_hw_set_domain_register(0x55555555);
  180. rt_cpu_tlb_set(MMUTable);
  181. rt_cpu_mmu_enable();
  182. rt_hw_cpu_icache_enable();
  183. rt_hw_cpu_dcache_enable();
  184. }