mcfmmu.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based upon linux/arch/m68k/mm/sun3mmu.c
  4. * Based upon linux/arch/ppc/mm/mmu_context.c
  5. *
  6. * Implementations of mm routines specific to the Coldfire MMU.
  7. *
  8. * Copyright (c) 2008 Freescale Semiconductor, Inc.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/mm.h>
  13. #include <linux/init.h>
  14. #include <linux/string.h>
  15. #include <linux/memblock.h>
  16. #include <asm/setup.h>
  17. #include <asm/page.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/mcf_pgalloc.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/pgalloc.h>
  22. #define KMAPAREA(x) ((x >= VMALLOC_START) && (x < KMAP_END))
  23. mm_context_t next_mmu_context;
  24. unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];
  25. atomic_t nr_free_contexts;
  26. struct mm_struct *context_mm[LAST_CONTEXT+1];
  27. unsigned long num_pages;
  28. /*
  29. * ColdFire paging_init derived from sun3.
  30. */
  31. void __init paging_init(void)
  32. {
  33. pgd_t *pg_dir;
  34. pte_t *pg_table;
  35. unsigned long address, size;
  36. unsigned long next_pgtable, bootmem_end;
  37. unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
  38. int i;
  39. empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  40. if (!empty_zero_page)
  41. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  42. __func__, PAGE_SIZE, PAGE_SIZE);
  43. pg_dir = swapper_pg_dir;
  44. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  45. size = num_pages * sizeof(pte_t);
  46. size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1);
  47. next_pgtable = (unsigned long) memblock_alloc(size, PAGE_SIZE);
  48. if (!next_pgtable)
  49. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  50. __func__, size, PAGE_SIZE);
  51. bootmem_end = (next_pgtable + size + PAGE_SIZE) & PAGE_MASK;
  52. pg_dir += PAGE_OFFSET >> PGDIR_SHIFT;
  53. address = PAGE_OFFSET;
  54. while (address < (unsigned long)high_memory) {
  55. pg_table = (pte_t *) next_pgtable;
  56. next_pgtable += PTRS_PER_PTE * sizeof(pte_t);
  57. pgd_val(*pg_dir) = (unsigned long) pg_table;
  58. pg_dir++;
  59. /* now change pg_table to kernel virtual addresses */
  60. for (i = 0; i < PTRS_PER_PTE; ++i, ++pg_table) {
  61. pte_t pte = pfn_pte(virt_to_pfn(address), PAGE_INIT);
  62. if (address >= (unsigned long) high_memory)
  63. pte_val(pte) = 0;
  64. set_pte(pg_table, pte);
  65. address += PAGE_SIZE;
  66. }
  67. }
  68. current->mm = NULL;
  69. max_zone_pfn[ZONE_DMA] = PFN_DOWN(_ramend);
  70. free_area_init(max_zone_pfn);
  71. }
  72. int cf_tlb_miss(struct pt_regs *regs, int write, int dtlb, int extension_word)
  73. {
  74. unsigned long flags, mmuar, mmutr;
  75. struct mm_struct *mm;
  76. pgd_t *pgd;
  77. p4d_t *p4d;
  78. pud_t *pud;
  79. pmd_t *pmd;
  80. pte_t *pte;
  81. int asid;
  82. local_irq_save(flags);
  83. mmuar = (dtlb) ? mmu_read(MMUAR) :
  84. regs->pc + (extension_word * sizeof(long));
  85. mm = (!user_mode(regs) && KMAPAREA(mmuar)) ? &init_mm : current->mm;
  86. if (!mm) {
  87. local_irq_restore(flags);
  88. return -1;
  89. }
  90. pgd = pgd_offset(mm, mmuar);
  91. if (pgd_none(*pgd)) {
  92. local_irq_restore(flags);
  93. return -1;
  94. }
  95. p4d = p4d_offset(pgd, mmuar);
  96. if (p4d_none(*p4d)) {
  97. local_irq_restore(flags);
  98. return -1;
  99. }
  100. pud = pud_offset(p4d, mmuar);
  101. if (pud_none(*pud)) {
  102. local_irq_restore(flags);
  103. return -1;
  104. }
  105. pmd = pmd_offset(pud, mmuar);
  106. if (pmd_none(*pmd)) {
  107. local_irq_restore(flags);
  108. return -1;
  109. }
  110. pte = (KMAPAREA(mmuar)) ? pte_offset_kernel(pmd, mmuar)
  111. : pte_offset_map(pmd, mmuar);
  112. if (pte_none(*pte) || !pte_present(*pte)) {
  113. local_irq_restore(flags);
  114. return -1;
  115. }
  116. if (write) {
  117. if (!pte_write(*pte)) {
  118. local_irq_restore(flags);
  119. return -1;
  120. }
  121. set_pte(pte, pte_mkdirty(*pte));
  122. }
  123. set_pte(pte, pte_mkyoung(*pte));
  124. asid = mm->context & 0xff;
  125. if (!pte_dirty(*pte) && !KMAPAREA(mmuar))
  126. set_pte(pte, pte_wrprotect(*pte));
  127. mmutr = (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) | MMUTR_V;
  128. if ((mmuar < TASK_UNMAPPED_BASE) || (mmuar >= TASK_SIZE))
  129. mmutr |= (pte->pte & CF_PAGE_MMUTR_MASK) >> CF_PAGE_MMUTR_SHIFT;
  130. mmu_write(MMUTR, mmutr);
  131. mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) |
  132. ((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X);
  133. if (dtlb)
  134. mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA);
  135. else
  136. mmu_write(MMUOR, MMUOR_ITLB | MMUOR_ACC | MMUOR_UAA);
  137. local_irq_restore(flags);
  138. return 0;
  139. }
  140. void __init cf_bootmem_alloc(void)
  141. {
  142. unsigned long memstart;
  143. /* _rambase and _ramend will be naturally page aligned */
  144. m68k_memory[0].addr = _rambase;
  145. m68k_memory[0].size = _ramend - _rambase;
  146. memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0,
  147. MEMBLOCK_NONE);
  148. /* compute total pages in system */
  149. num_pages = PFN_DOWN(_ramend - _rambase);
  150. /* page numbers */
  151. memstart = PAGE_ALIGN(_ramstart);
  152. min_low_pfn = PFN_DOWN(_rambase);
  153. max_pfn = max_low_pfn = PFN_DOWN(_ramend);
  154. high_memory = (void *)_ramend;
  155. /* Reserve kernel text/data/bss */
  156. memblock_reserve(_rambase, memstart - _rambase);
  157. m68k_virt_to_node_shift = fls(_ramend - 1) - 6;
  158. module_fixup(NULL, __start_fixup, __stop_fixup);
  159. /* setup node data */
  160. m68k_setup_node(0);
  161. }
  162. /*
  163. * Initialize the context management stuff.
  164. * The following was taken from arch/ppc/mmu_context.c
  165. */
  166. void __init cf_mmu_context_init(void)
  167. {
  168. /*
  169. * Some processors have too few contexts to reserve one for
  170. * init_mm, and require using context 0 for a normal task.
  171. * Other processors reserve the use of context zero for the kernel.
  172. * This code assumes FIRST_CONTEXT < 32.
  173. */
  174. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  175. next_mmu_context = FIRST_CONTEXT;
  176. atomic_set(&nr_free_contexts, LAST_CONTEXT - FIRST_CONTEXT + 1);
  177. }
  178. /*
  179. * Steal a context from a task that has one at the moment.
  180. * This isn't an LRU system, it just frees up each context in
  181. * turn (sort-of pseudo-random replacement :). This would be the
  182. * place to implement an LRU scheme if anyone was motivated to do it.
  183. * -- paulus
  184. */
  185. void steal_context(void)
  186. {
  187. struct mm_struct *mm;
  188. /*
  189. * free up context `next_mmu_context'
  190. * if we shouldn't free context 0, don't...
  191. */
  192. if (next_mmu_context < FIRST_CONTEXT)
  193. next_mmu_context = FIRST_CONTEXT;
  194. mm = context_mm[next_mmu_context];
  195. flush_tlb_mm(mm);
  196. destroy_context(mm);
  197. }
  198. static const pgprot_t protection_map[16] = {
  199. [VM_NONE] = PAGE_NONE,
  200. [VM_READ] = __pgprot(CF_PAGE_VALID |
  201. CF_PAGE_ACCESSED |
  202. CF_PAGE_READABLE),
  203. [VM_WRITE] = __pgprot(CF_PAGE_VALID |
  204. CF_PAGE_ACCESSED |
  205. CF_PAGE_WRITABLE),
  206. [VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  207. CF_PAGE_ACCESSED |
  208. CF_PAGE_READABLE |
  209. CF_PAGE_WRITABLE),
  210. [VM_EXEC] = __pgprot(CF_PAGE_VALID |
  211. CF_PAGE_ACCESSED |
  212. CF_PAGE_EXEC),
  213. [VM_EXEC | VM_READ] = __pgprot(CF_PAGE_VALID |
  214. CF_PAGE_ACCESSED |
  215. CF_PAGE_READABLE |
  216. CF_PAGE_EXEC),
  217. [VM_EXEC | VM_WRITE] = __pgprot(CF_PAGE_VALID |
  218. CF_PAGE_ACCESSED |
  219. CF_PAGE_WRITABLE |
  220. CF_PAGE_EXEC),
  221. [VM_EXEC | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  222. CF_PAGE_ACCESSED |
  223. CF_PAGE_READABLE |
  224. CF_PAGE_WRITABLE |
  225. CF_PAGE_EXEC),
  226. [VM_SHARED] = PAGE_NONE,
  227. [VM_SHARED | VM_READ] = __pgprot(CF_PAGE_VALID |
  228. CF_PAGE_ACCESSED |
  229. CF_PAGE_READABLE),
  230. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  231. [VM_SHARED | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  232. CF_PAGE_ACCESSED |
  233. CF_PAGE_READABLE |
  234. CF_PAGE_SHARED),
  235. [VM_SHARED | VM_EXEC] = __pgprot(CF_PAGE_VALID |
  236. CF_PAGE_ACCESSED |
  237. CF_PAGE_EXEC),
  238. [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(CF_PAGE_VALID |
  239. CF_PAGE_ACCESSED |
  240. CF_PAGE_READABLE |
  241. CF_PAGE_EXEC),
  242. [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(CF_PAGE_VALID |
  243. CF_PAGE_ACCESSED |
  244. CF_PAGE_SHARED |
  245. CF_PAGE_EXEC),
  246. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  247. CF_PAGE_ACCESSED |
  248. CF_PAGE_READABLE |
  249. CF_PAGE_SHARED |
  250. CF_PAGE_EXEC)
  251. };
  252. DECLARE_VM_GET_PAGE_PROT