mmu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * COPYRIGHT (C) 2013-2014, Shanghai Real-Thread Technology Co., Ltd
  3. *
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <rtthread.h>
  21. #include <rthw.h>
  22. #include <board.h>
  23. #include "cp15.h"
  24. #define DESC_SEC (0x2)
  25. #define CB (3<<2) //cache_on, write_back
  26. #define CNB (2<<2) //cache_on, write_through
  27. #define NCB (1<<2) //cache_off,WR_BUF on
  28. #define NCNB (0<<2) //cache_off,WR_BUF off
  29. #define AP_RW (3<<10) //supervisor=RW, user=RW
  30. #define AP_RO (2<<10) //supervisor=RW, user=RO
  31. #define XN (1<<4) //eXecute Never
  32. #define DOMAIN_FAULT (0x0)
  33. #define DOMAIN_CHK (0x1)
  34. #define DOMAIN_NOTCHK (0x3)
  35. #define DOMAIN0 (0x0<<5)
  36. #define DOMAIN1 (0x1<<5)
  37. #define DOMAIN0_ATTR (DOMAIN_CHK<<0)
  38. #define DOMAIN1_ATTR (DOMAIN_FAULT<<2)
  39. /* Read/Write, cache, write back */
  40. #define RW_CB (AP_RW|DOMAIN0|CB|DESC_SEC)
  41. /* Read/Write, cache, write through */
  42. #define RW_CNB (AP_RW|DOMAIN0|CNB|DESC_SEC)
  43. /* Read/Write, device type */
  44. #define RW_NCB (AP_RW|DOMAIN0|NCB|DESC_SEC)
  45. /* Read/Write strongly ordered type */
  46. #define RW_NCNB (AP_RW|DOMAIN0|NCNB|DESC_SEC)
  47. /* Read/Write without cache and write buffer, no execute */
  48. #define RW_NCNBXN (AP_RW|DOMAIN0|NCNB|DESC_SEC|XN)
  49. /* Read/Write without cache and write buffer */
  50. #define RW_FAULT (AP_RW|DOMAIN1|NCNB|DESC_SEC)
  51. void rt_hw_cpu_dump_page_table(rt_uint32_t *ptb)
  52. {
  53. int i;
  54. int fcnt = 0;
  55. rt_kprintf("page table@%p\n", ptb);
  56. for (i = 0; i < 1024*4; i++)
  57. {
  58. rt_uint32_t pte1 = ptb[i];
  59. if ((pte1 & 0x3) == 0)
  60. {
  61. rt_kprintf("%03x: ", i);
  62. fcnt++;
  63. if (fcnt == 16)
  64. {
  65. rt_kprintf("fault\n");
  66. fcnt = 0;
  67. }
  68. continue;
  69. }
  70. if (fcnt != 0)
  71. {
  72. rt_kprintf("fault\n");
  73. fcnt = 0;
  74. }
  75. rt_kprintf("%03x: %08x: ", i, pte1);
  76. if ((pte1 & 0x3) == 0x3)
  77. {
  78. rt_kprintf("LPAE\n");
  79. }
  80. else if ((pte1 & 0x3) == 0x1)
  81. {
  82. rt_kprintf("pte,ns:%d,domain:%d\n",
  83. (pte1 >> 3) & 0x1, (pte1 >> 5) & 0xf);
  84. /*
  85. *rt_hw_cpu_dump_page_table_2nd((void*)((pte1 & 0xfffffc000)
  86. * - 0x80000000 + 0xC0000000));
  87. */
  88. }
  89. else if (pte1 & (1 << 18))
  90. {
  91. rt_kprintf("super section,ns:%d,ap:%x,xn:%d,texcb:%02x\n",
  92. (pte1 >> 19) & 0x1,
  93. ((pte1 >> 13) | (pte1 >> 10))& 0xf,
  94. (pte1 >> 4) & 0x1,
  95. ((pte1 >> 10) | (pte1 >> 2)) & 0x1f);
  96. }
  97. else
  98. {
  99. rt_kprintf("section,ns:%d,ap:%x,"
  100. "xn:%d,texcb:%02x,domain:%d\n",
  101. (pte1 >> 19) & 0x1,
  102. ((pte1 >> 13) | (pte1 >> 10))& 0xf,
  103. (pte1 >> 4) & 0x1,
  104. (((pte1 & (0x7 << 12)) >> 10) |
  105. ((pte1 & 0x0c) >> 2)) & 0x1f,
  106. (pte1 >> 5) & 0xf);
  107. }
  108. }
  109. }
  110. /* level1 page table, each entry for 1MB memory. */
  111. /* MMUTable is the name used by codes of Xilinx */
  112. volatile unsigned long MMUTable[4*1024] SECTION("mmu_tbl") __attribute__((aligned(16*1024)));
  113. void rt_hw_mmu_setmtt(rt_uint32_t vaddrStart,
  114. rt_uint32_t vaddrEnd,
  115. rt_uint32_t paddrStart,
  116. rt_uint32_t attr)
  117. {
  118. volatile rt_uint32_t *pTT;
  119. volatile int i, nSec;
  120. pTT = (rt_uint32_t *)MMUTable + (vaddrStart >> 20);
  121. nSec = (vaddrEnd >> 20) - (vaddrStart >> 20);
  122. for(i = 0; i <= nSec; i++)
  123. {
  124. *pTT = attr | (((paddrStart >> 20) + i) << 20);
  125. pTT++;
  126. }
  127. }
  128. unsigned long rt_hw_set_domain_register(unsigned long domain_val)
  129. {
  130. unsigned long old_domain;
  131. asm volatile ("mrc p15, 0, %0, c3, c0\n" : "=r" (old_domain));
  132. asm volatile ("mcr p15, 0, %0, c3, c0\n" : :"r" (domain_val) : "memory");
  133. return old_domain;
  134. }
  135. void rt_hw_mmu_init(void)
  136. {
  137. extern rt_uint32_t __text_start;
  138. rt_hw_cpu_dcache_disable();
  139. rt_hw_cpu_icache_disable();
  140. rt_cpu_mmu_disable();
  141. /* set page table */
  142. /* no access to the memory below .text */
  143. /* 128M cached DDR memory */
  144. rt_hw_mmu_setmtt((rt_uint32_t)&__text_start, 0x20000000-1,
  145. 0x1ff00000, RW_CB);
  146. /* PL region */
  147. rt_hw_mmu_setmtt(0x40000000, 0xBFFFFFFF, 0x40000000, RW_NCNBXN);
  148. /* IOP registers */
  149. rt_hw_mmu_setmtt(0xE0000000, 0xE02FFFFF, 0xE0000000, RW_NCNBXN);
  150. /* no access to the SMC memory(enable it if you want) */
  151. /* SLCR, PS and CPU private registers, note we map more memory space as the
  152. * entry is 1MB in size. */
  153. rt_hw_mmu_setmtt(0xF8000000, 0xF8FFFFFF, 0xF8000000, RW_NCNBXN);
  154. /*rt_hw_cpu_dump_page_table(MMUTable);*/
  155. /* become clients for all domains */
  156. rt_hw_set_domain_register(0x55555555);
  157. rt_cpu_tlb_set(MMUTable);
  158. rt_cpu_mmu_enable();
  159. rt_hw_cpu_icache_enable();
  160. rt_hw_cpu_dcache_enable();
  161. }