mmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 volatile
  35. {
  36. mcr p15, 0, value, c8, c7, 0
  37. }
  38. value = 0x55555555;
  39. __asm volatile
  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 volatile
  48. {
  49. mcr p15,0, i, c3, c0, 0
  50. }
  51. }
  52. void mmu_enable()
  53. {
  54. register rt_uint32_t value;
  55. __asm volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile
  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 volatile ("mcr p15, 0, %0, c8, c7, 0"::"r"(value));
  216. value = 0x55555555;
  217. asm volatile ("mcr p15, 0, %0, c3, c0, 0"::"r"(value));
  218. asm volatile ("mcr p15, 0, %0, c2, c0, 0"::"r"(i));
  219. }
  220. void mmu_set_domain(register rt_uint32_t i)
  221. {
  222. asm volatile ("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 volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  229. i |= 0x1;
  230. i |= (1 << 13); /* High exception vectors selected, address range = 0xFFFF0000-0xFFFF001C */
  231. /* S R bit=1 0 for system protection */
  232. i |= (1 << 8);
  233. i &= ~(1 << 9);
  234. /* write back to control register */
  235. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  236. }
  237. void mmu_disable()
  238. {
  239. register rt_uint32_t i;
  240. /* read control register */
  241. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  242. i &= ~0x1;
  243. /* write back to control register */
  244. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  245. }
  246. void mmu_enable_icache()
  247. {
  248. register rt_uint32_t i;
  249. /* read control register */
  250. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  251. i |= (1 << 12);
  252. /* write back to control register */
  253. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  254. }
  255. void mmu_enable_dcache()
  256. {
  257. register rt_uint32_t i;
  258. /* read control register */
  259. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  260. i |= (1 << 2);
  261. /* write back to control register */
  262. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  263. }
  264. void mmu_disable_icache()
  265. {
  266. register rt_uint32_t i;
  267. /* read control register */
  268. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  269. i &= ~(1 << 12);
  270. /* write back to control register */
  271. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  272. }
  273. void mmu_disable_dcache()
  274. {
  275. register rt_uint32_t i;
  276. /* read control register */
  277. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  278. i &= ~(1 << 2);
  279. /* write back to control register */
  280. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  281. }
  282. void mmu_enable_alignfault()
  283. {
  284. register rt_uint32_t i;
  285. /* read control register */
  286. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  287. i |= (1 << 1);
  288. /* write back to control register */
  289. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  290. }
  291. void mmu_disable_alignfault()
  292. {
  293. register rt_uint32_t i;
  294. /* read control register */
  295. asm volatile ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
  296. i &= ~(1 << 1);
  297. /* write back to control register */
  298. asm volatile ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
  299. }
  300. void mmu_clean_invalidated_cache_index(int index)
  301. {
  302. asm volatile ("mcr p15, 0, %0, c7, c14, 2": :"r" (index));
  303. }
  304. void mmu_clean_invalidated_dcache(rt_uint32_t buffer, rt_uint32_t size)
  305. {
  306. unsigned int ptr;
  307. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  308. while(ptr < buffer + size)
  309. {
  310. asm volatile ("mcr p15, 0, %0, c7, c14, 1": :"r" (ptr));
  311. ptr += CACHE_LINE_SIZE;
  312. }
  313. }
  314. void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size)
  315. {
  316. unsigned int ptr;
  317. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  318. while (ptr < buffer + size)
  319. {
  320. asm volatile ("mcr p15, 0, %0, c7, c10, 1": :"r" (ptr));
  321. ptr += CACHE_LINE_SIZE;
  322. }
  323. }
  324. void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size)
  325. {
  326. unsigned int ptr;
  327. ptr = buffer & ~(CACHE_LINE_SIZE - 1);
  328. while (ptr < buffer + size)
  329. {
  330. asm volatile ("mcr p15, 0, %0, c7, c6, 1": :"r" (ptr));
  331. ptr += CACHE_LINE_SIZE;
  332. }
  333. }
  334. void mmu_invalidate_tlb()
  335. {
  336. asm volatile ("mcr p15, 0, %0, c8, c7, 0": :"r" (0));
  337. }
  338. void mmu_invalidate_icache()
  339. {
  340. asm volatile ("mcr p15, 0, %0, c7, c5, 0": :"r" (0));
  341. }
  342. void mmu_invalidate_dcache_all()
  343. {
  344. asm volatile ("mcr p15, 0, %0, c7, c6, 0": :"r" (0));
  345. }
  346. #endif
  347. /* level1 page table */
  348. static volatile unsigned int _pgd_table[4*1024] ALIGN(16*1024);
  349. /*
  350. * level2 page table
  351. * RT_MMU_PTE_SIZE must be 1024*n
  352. */
  353. static volatile unsigned int _pte_table[RT_MMU_PTE_SIZE] ALIGN(1*1024);
  354. void mmu_create_pgd(struct mem_desc *mdesc)
  355. {
  356. volatile rt_uint32_t *pTT;
  357. volatile int i, nSec;
  358. pTT = (rt_uint32_t *)_pgd_table + (mdesc->vaddr_start >> 20);
  359. nSec = (mdesc->vaddr_end >> 20) - (mdesc->vaddr_start >> 20);
  360. for(i = 0; i <= nSec; i++)
  361. {
  362. *pTT = mdesc->sect_attr | (((mdesc->paddr_start >> 20) + i) << 20);
  363. pTT++;
  364. }
  365. }
  366. void mmu_create_pte(struct mem_desc *mdesc)
  367. {
  368. volatile rt_uint32_t *pTT;
  369. volatile rt_uint32_t *p_pteentry;
  370. int i;
  371. rt_uint32_t vaddr;
  372. rt_uint32_t total_page = 0;
  373. rt_uint32_t pte_offset = 0;
  374. rt_uint32_t sect_attr = 0;
  375. total_page = (mdesc->vaddr_end >> 12) - (mdesc->vaddr_start >> 12) + 1;
  376. pte_offset = mdesc->sect_attr & 0xfffffc00;
  377. sect_attr = mdesc->sect_attr & 0x3ff;
  378. vaddr = mdesc->vaddr_start;
  379. for(i = 0; i < total_page; i++)
  380. {
  381. pTT = (rt_uint32_t *)_pgd_table + (vaddr >> 20);
  382. if (*pTT == 0) /* Level 1 page table item not used, now update pgd item */
  383. {
  384. *pTT = pte_offset | sect_attr;
  385. p_pteentry = (rt_uint32_t *)pte_offset +
  386. ((vaddr & 0x000ff000) >> 12);
  387. pte_offset += 1024;
  388. }
  389. else /* using old Level 1 page table item */
  390. {
  391. p_pteentry = (rt_uint32_t *)(*pTT & 0xfffffc00) +
  392. ((vaddr & 0x000ff000) >> 12);
  393. }
  394. *p_pteentry = mdesc->page_attr | (((mdesc->paddr_start >> 12) + i) << 12);
  395. vaddr += 0x1000;
  396. }
  397. }
  398. static void build_pte_mem_desc(struct mem_desc *mdesc, rt_uint32_t size)
  399. {
  400. rt_uint32_t pte_offset = 0;
  401. rt_uint32_t nsec = 0;
  402. /* set page table */
  403. for (; size > 0; size--)
  404. {
  405. if (mdesc->mapped_mode == PAGE_MAPPED)
  406. {
  407. nsec = (RT_ALIGN(mdesc->vaddr_end, 0x100000) - RT_ALIGN_DOWN(mdesc->vaddr_start, 0x100000)) >> 20;
  408. mdesc->sect_attr |= (((rt_uint32_t)_pte_table)& 0xfffffc00) + pte_offset;
  409. pte_offset += nsec << 10;
  410. }
  411. if (pte_offset >= RT_MMU_PTE_SIZE)
  412. {
  413. rt_kprintf("PTE table size too little\n");
  414. RT_ASSERT(0);
  415. }
  416. mdesc++;
  417. }
  418. }
  419. void rt_hw_mmu_init(struct mem_desc *mdesc, rt_uint32_t size)
  420. {
  421. /* disable I/D cache */
  422. mmu_disable_dcache();
  423. mmu_disable_icache();
  424. mmu_disable();
  425. mmu_invalidate_tlb();
  426. /* clear pgd and pte table */
  427. rt_memset((void *)_pgd_table, 0, 16*1024);
  428. rt_memset((void *)_pte_table, 0, RT_MMU_PTE_SIZE);
  429. build_pte_mem_desc(mdesc, size);
  430. /* set page table */
  431. for (; size > 0; size--)
  432. {
  433. if (mdesc->mapped_mode == SECT_MAPPED)
  434. {
  435. mmu_create_pgd(mdesc);
  436. }
  437. else
  438. {
  439. mmu_create_pte(mdesc);
  440. }
  441. mdesc++;
  442. }
  443. /* set MMU table address */
  444. mmu_setttbase((rt_uint32_t)_pgd_table);
  445. /* enables MMU */
  446. mmu_enable();
  447. /* enable Instruction Cache */
  448. mmu_enable_icache();
  449. /* enable Data Cache */
  450. mmu_enable_dcache();
  451. mmu_invalidate_icache();
  452. mmu_invalidate_dcache_all();
  453. }