mmu.c 8.7 KB

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