mmu.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. */
  13. #include "mmu.h"
  14. #ifdef __CC_ARM
  15. void mmu_setttbase(rt_uint32_t i)
  16. {
  17. register rt_uint32_t value;
  18. /* Invalidates all TLBs.Domain access is selected as
  19. * client by configuring domain access register,
  20. * in that case access controlled by permission value
  21. * set by page table entry
  22. */
  23. value = 0;
  24. __asm
  25. {
  26. mcr p15, 0, value, c8, c7, 0
  27. }
  28. value = 0x55555555;
  29. __asm
  30. {
  31. mcr p15, 0, value, c3, c0, 0
  32. mcr p15, 0, i, c2, c0, 0
  33. }
  34. }
  35. void mmu_set_domain(rt_uint32_t i)
  36. {
  37. __asm
  38. {
  39. mcr p15,0, i, c3, c0, 0
  40. }
  41. }
  42. void mmu_enable()
  43. {
  44. register rt_uint32_t value;
  45. __asm
  46. {
  47. mrc p15, 0, value, c1, c0, 0
  48. orr value, value, #0x01
  49. mcr p15, 0, value, c1, c0, 0
  50. }
  51. }
  52. void mmu_disable()
  53. {
  54. register rt_uint32_t value;
  55. __asm
  56. {
  57. mrc p15, 0, value, c1, c0, 0
  58. bic value, value, #0x01
  59. mcr p15, 0, value, c1, c0, 0
  60. }
  61. }
  62. void mmu_enable_icache()
  63. {
  64. register rt_uint32_t value;
  65. __asm
  66. {
  67. mrc p15, 0, value, c1, c0, 0
  68. orr value, value, #0x1000
  69. mcr p15, 0, value, c1, c0, 0
  70. }
  71. }
  72. void mmu_enable_dcache()
  73. {
  74. register rt_uint32_t value;
  75. __asm
  76. {
  77. mrc p15, 0, value, c1, c0, 0
  78. orr value, value, #0x04
  79. mcr p15, 0, value, c1, c0, 0
  80. }
  81. }
  82. void mmu_disable_icache()
  83. {
  84. register rt_uint32_t value;
  85. __asm
  86. {
  87. mrc p15, 0, value, c1, c0, 0
  88. bic value, value, #0x1000
  89. mcr p15, 0, value, c1, c0, 0
  90. }
  91. }
  92. void mmu_disable_dcache()
  93. {
  94. register rt_uint32_t value;
  95. __asm
  96. {
  97. mrc p15, 0, value, c1, c0, 0
  98. bic value, value, #0x04
  99. mcr p15, 0, value, c1, c0, 0
  100. }
  101. }
  102. void mmu_enable_alignfault()
  103. {
  104. register rt_uint32_t value;
  105. __asm
  106. {
  107. mrc p15, 0, value, c1, c0, 0
  108. orr value, value, #0x02
  109. mcr p15, 0, value, c1, c0, 0
  110. }
  111. }
  112. void mmu_disable_alignfault()
  113. {
  114. register rt_uint32_t value;
  115. __asm
  116. {
  117. mrc p15, 0, value, c1, c0, 0
  118. bic value, value, #0x02
  119. mcr p15, 0, value, c1, c0, 0
  120. }
  121. }
  122. void mmu_clean_invalidated_cache_index(int index)
  123. {
  124. __asm
  125. {
  126. mcr p15, 0, index, c7, c14, 2
  127. }
  128. }
  129. void mmu_clean_invalidated_dcache(rt_uint32_t buffer, rt_uint32_t size)
  130. {
  131. unsigned int ptr;
  132. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  133. while(ptr < buffer + size)
  134. {
  135. __asm
  136. {
  137. MCR p15, 0, ptr, c7, c14, 1
  138. }
  139. ptr += CACHE_LINE_SIZE;
  140. }
  141. }
  142. void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size)
  143. {
  144. unsigned int ptr;
  145. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  146. while (ptr < buffer + size)
  147. {
  148. __asm
  149. {
  150. MCR p15, 0, ptr, c7, c10, 1
  151. }
  152. ptr += CACHE_LINE_SIZE;
  153. }
  154. }
  155. void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size)
  156. {
  157. unsigned int ptr;
  158. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  159. while (ptr < buffer + size)
  160. {
  161. __asm
  162. {
  163. MCR p15, 0, ptr, c7, c6, 1
  164. }
  165. ptr += CACHE_LINE_SIZE;
  166. }
  167. }
  168. void mmu_invalidate_tlb()
  169. {
  170. register rt_uint32_t value;
  171. value = 0;
  172. __asm
  173. {
  174. mcr p15, 0, value, c8, c7, 0
  175. }
  176. }
  177. void mmu_invalidate_icache()
  178. {
  179. register rt_uint32_t value;
  180. value = 0;
  181. __asm
  182. {
  183. mcr p15, 0, value, c7, c5, 0
  184. }
  185. }
  186. void mmu_invalidate_dcache_all()
  187. {
  188. register rt_uint32_t value;
  189. value = 0;
  190. __asm
  191. {
  192. mcr p15, 0, value, c7, c6, 0
  193. }
  194. }
  195. #elif defined(__GNUC__)
  196. void mmu_setttbase(register rt_uint32_t i)
  197. {
  198. register rt_uint32_t value;
  199. /* Invalidates all TLBs.Domain access is selected as
  200. * client by configuring domain access register,
  201. * in that case access controlled by permission value
  202. * set by page table entry
  203. */
  204. value = 0;
  205. asm ("mcr p15, 0, %0, c8, c7, 0"::"r"(value));
  206. value = 0x55555555;
  207. asm ("mcr p15, 0, %0, c3, c0, 0"::"r"(value));
  208. asm ("mcr p15, 0, %0, c2, c0, 0"::"r"(i));
  209. }
  210. void mmu_set_domain(register rt_uint32_t i)
  211. {
  212. asm ("mcr p15,0, %0, c3, c0, 0": :"r" (i));
  213. }
  214. void mmu_enable()
  215. {
  216. register rt_uint32_t i;
  217. /* read control register */
  218. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  219. i |= 0x1;
  220. /* write back to control register */
  221. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  222. }
  223. void mmu_disable()
  224. {
  225. register rt_uint32_t i;
  226. /* read control register */
  227. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  228. i &= ~0x1;
  229. /* write back to control register */
  230. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  231. }
  232. void mmu_enable_icache()
  233. {
  234. register rt_uint32_t i;
  235. /* read control register */
  236. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  237. i |= (1 << 12);
  238. /* write back to control register */
  239. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  240. }
  241. void mmu_enable_dcache()
  242. {
  243. register rt_uint32_t i;
  244. /* read control register */
  245. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  246. i |= (1 << 2);
  247. /* write back to control register */
  248. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  249. }
  250. void mmu_disable_icache()
  251. {
  252. register rt_uint32_t i;
  253. /* read control register */
  254. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  255. i &= ~(1 << 12);
  256. /* write back to control register */
  257. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  258. }
  259. void mmu_disable_dcache()
  260. {
  261. register rt_uint32_t i;
  262. /* read control register */
  263. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  264. i &= ~(1 << 2);
  265. /* write back to control register */
  266. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  267. }
  268. void mmu_enable_alignfault()
  269. {
  270. register rt_uint32_t i;
  271. /* read control register */
  272. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  273. i |= (1 << 1);
  274. /* write back to control register */
  275. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  276. }
  277. void mmu_disable_alignfault()
  278. {
  279. register rt_uint32_t i;
  280. /* read control register */
  281. asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  282. i &= ~(1 << 1);
  283. /* write back to control register */
  284. asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  285. }
  286. void mmu_clean_invalidated_cache_index(int index)
  287. {
  288. asm ("mcr p15, 0, %0, c7, c14, 2": :"r" (index));
  289. }
  290. void mmu_clean_invalidated_dcache(rt_uint32_t buffer, rt_uint32_t size)
  291. {
  292. unsigned int ptr;
  293. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  294. while(ptr < buffer + size)
  295. {
  296. asm ("mcr p15, 0, %0, c7, c14, 1": :"r" (ptr));
  297. ptr += CACHE_LINE_SIZE;
  298. }
  299. }
  300. void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size)
  301. {
  302. unsigned int ptr;
  303. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  304. while (ptr < buffer + size)
  305. {
  306. asm ("mcr p15, 0, %0, c7, c10, 1": :"r" (ptr));
  307. ptr += CACHE_LINE_SIZE;
  308. }
  309. }
  310. void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size)
  311. {
  312. unsigned int ptr;
  313. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  314. while (ptr < buffer + size)
  315. {
  316. asm ("mcr p15, 0, %0, c7, c6, 1": :"r" (ptr));
  317. ptr += CACHE_LINE_SIZE;
  318. }
  319. }
  320. void mmu_invalidate_tlb()
  321. {
  322. asm ("mcr p15, 0, %0, c8, c7, 0": :"r" (0));
  323. }
  324. void mmu_invalidate_icache()
  325. {
  326. asm ("mcr p15, 0, %0, c7, c5, 0": :"r" (0));
  327. }
  328. void mmu_invalidate_dcache_all()
  329. {
  330. asm ("mcr p15, 0, %0, c7, c6, 0": :"r" (0));
  331. }
  332. #endif
  333. /* level1 page table */
  334. static volatile unsigned int _page_table[4*1024] __attribute__((aligned(16*1024)));
  335. void mmu_setmtt(rt_uint32_t vaddrStart, rt_uint32_t vaddrEnd, rt_uint32_t paddrStart, rt_uint32_t attr)
  336. {
  337. volatile rt_uint32_t *pTT;
  338. volatile int i,nSec;
  339. pTT=(rt_uint32_t *)_page_table+(vaddrStart>>20);
  340. nSec=(vaddrEnd>>20)-(vaddrStart>>20);
  341. for(i=0;i<=nSec;i++)
  342. {
  343. *pTT = attr |(((paddrStart>>20)+i)<<20);
  344. pTT++;
  345. }
  346. }
  347. void rt_hw_mmu_init(struct mem_desc *mdesc, rt_uint32_t size)
  348. {
  349. /* disable I/D cache */
  350. mmu_disable_dcache();
  351. mmu_disable_icache();
  352. mmu_disable();
  353. mmu_invalidate_tlb();
  354. /* set page table */
  355. for (; size > 0; size--)
  356. {
  357. mmu_setmtt(mdesc->vaddr_start, mdesc->vaddr_end,
  358. mdesc->paddr_start, mdesc->attr);
  359. mdesc++;
  360. }
  361. /* set MMU table address */
  362. mmu_setttbase((rt_uint32_t)_page_table);
  363. /* enables MMU */
  364. mmu_enable();
  365. /* enable Instruction Cache */
  366. mmu_enable_icache();
  367. /* enable Data Cache */
  368. mmu_enable_dcache();
  369. mmu_invalidate_icache();
  370. mmu_invalidate_dcache_all();
  371. }