mcfmmu.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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;
  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. pg_dir += PAGE_OFFSET >> PGDIR_SHIFT;
  52. address = PAGE_OFFSET;
  53. while (address < (unsigned long)high_memory) {
  54. pg_table = (pte_t *) next_pgtable;
  55. next_pgtable += PTRS_PER_PTE * sizeof(pte_t);
  56. pgd_val(*pg_dir) = (unsigned long) pg_table;
  57. pg_dir++;
  58. /* now change pg_table to kernel virtual addresses */
  59. for (i = 0; i < PTRS_PER_PTE; ++i, ++pg_table) {
  60. pte_t pte = pfn_pte(virt_to_pfn((void *)address),
  61. 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 = NULL;
  81. int ret = -1;
  82. int asid;
  83. local_irq_save(flags);
  84. mmuar = (dtlb) ? mmu_read(MMUAR) :
  85. regs->pc + (extension_word * sizeof(long));
  86. mm = (!user_mode(regs) && KMAPAREA(mmuar)) ? &init_mm : current->mm;
  87. if (!mm)
  88. goto out;
  89. pgd = pgd_offset(mm, mmuar);
  90. if (pgd_none(*pgd))
  91. goto out;
  92. p4d = p4d_offset(pgd, mmuar);
  93. if (p4d_none(*p4d))
  94. goto out;
  95. pud = pud_offset(p4d, mmuar);
  96. if (pud_none(*pud))
  97. goto out;
  98. pmd = pmd_offset(pud, mmuar);
  99. if (pmd_none(*pmd))
  100. goto out;
  101. pte = (KMAPAREA(mmuar)) ? pte_offset_kernel(pmd, mmuar)
  102. : pte_offset_map(pmd, mmuar);
  103. if (!pte || pte_none(*pte) || !pte_present(*pte))
  104. goto out;
  105. if (write) {
  106. if (!pte_write(*pte))
  107. goto out;
  108. set_pte(pte, pte_mkdirty(*pte));
  109. }
  110. set_pte(pte, pte_mkyoung(*pte));
  111. asid = mm->context & 0xff;
  112. if (!pte_dirty(*pte) && !KMAPAREA(mmuar))
  113. set_pte(pte, pte_wrprotect(*pte));
  114. mmutr = (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) | MMUTR_V;
  115. if ((mmuar < TASK_UNMAPPED_BASE) || (mmuar >= TASK_SIZE))
  116. mmutr |= (pte->pte & CF_PAGE_MMUTR_MASK) >> CF_PAGE_MMUTR_SHIFT;
  117. mmu_write(MMUTR, mmutr);
  118. mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) |
  119. ((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X);
  120. if (dtlb)
  121. mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA);
  122. else
  123. mmu_write(MMUOR, MMUOR_ITLB | MMUOR_ACC | MMUOR_UAA);
  124. ret = 0;
  125. out:
  126. if (pte && !KMAPAREA(mmuar))
  127. pte_unmap(pte);
  128. local_irq_restore(flags);
  129. return ret;
  130. }
  131. void __init cf_bootmem_alloc(void)
  132. {
  133. unsigned long memstart;
  134. /* _rambase and _ramend will be naturally page aligned */
  135. m68k_memory[0].addr = _rambase;
  136. m68k_memory[0].size = _ramend - _rambase;
  137. memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0,
  138. MEMBLOCK_NONE);
  139. /* compute total pages in system */
  140. num_pages = PFN_DOWN(_ramend - _rambase);
  141. /* page numbers */
  142. memstart = PAGE_ALIGN(_ramstart);
  143. min_low_pfn = PFN_DOWN(_rambase);
  144. max_pfn = max_low_pfn = PFN_DOWN(_ramend);
  145. high_memory = (void *)_ramend;
  146. /* Reserve kernel text/data/bss */
  147. memblock_reserve(_rambase, memstart - _rambase);
  148. m68k_virt_to_node_shift = fls(_ramend - 1) - 6;
  149. module_fixup(NULL, __start_fixup, __stop_fixup);
  150. /* setup node data */
  151. m68k_setup_node(0);
  152. }
  153. /*
  154. * Initialize the context management stuff.
  155. * The following was taken from arch/ppc/mmu_context.c
  156. */
  157. void __init cf_mmu_context_init(void)
  158. {
  159. /*
  160. * Some processors have too few contexts to reserve one for
  161. * init_mm, and require using context 0 for a normal task.
  162. * Other processors reserve the use of context zero for the kernel.
  163. * This code assumes FIRST_CONTEXT < 32.
  164. */
  165. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  166. next_mmu_context = FIRST_CONTEXT;
  167. atomic_set(&nr_free_contexts, LAST_CONTEXT - FIRST_CONTEXT + 1);
  168. }
  169. /*
  170. * Steal a context from a task that has one at the moment.
  171. * This isn't an LRU system, it just frees up each context in
  172. * turn (sort-of pseudo-random replacement :). This would be the
  173. * place to implement an LRU scheme if anyone was motivated to do it.
  174. * -- paulus
  175. */
  176. void steal_context(void)
  177. {
  178. struct mm_struct *mm;
  179. /*
  180. * free up context `next_mmu_context'
  181. * if we shouldn't free context 0, don't...
  182. */
  183. if (next_mmu_context < FIRST_CONTEXT)
  184. next_mmu_context = FIRST_CONTEXT;
  185. mm = context_mm[next_mmu_context];
  186. flush_tlb_mm(mm);
  187. destroy_context(mm);
  188. }
  189. static const pgprot_t protection_map[16] = {
  190. [VM_NONE] = PAGE_NONE,
  191. [VM_READ] = __pgprot(CF_PAGE_VALID |
  192. CF_PAGE_ACCESSED |
  193. CF_PAGE_READABLE),
  194. [VM_WRITE] = __pgprot(CF_PAGE_VALID |
  195. CF_PAGE_ACCESSED |
  196. CF_PAGE_WRITABLE),
  197. [VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  198. CF_PAGE_ACCESSED |
  199. CF_PAGE_READABLE |
  200. CF_PAGE_WRITABLE),
  201. [VM_EXEC] = __pgprot(CF_PAGE_VALID |
  202. CF_PAGE_ACCESSED |
  203. CF_PAGE_EXEC),
  204. [VM_EXEC | VM_READ] = __pgprot(CF_PAGE_VALID |
  205. CF_PAGE_ACCESSED |
  206. CF_PAGE_READABLE |
  207. CF_PAGE_EXEC),
  208. [VM_EXEC | VM_WRITE] = __pgprot(CF_PAGE_VALID |
  209. CF_PAGE_ACCESSED |
  210. CF_PAGE_WRITABLE |
  211. CF_PAGE_EXEC),
  212. [VM_EXEC | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  213. CF_PAGE_ACCESSED |
  214. CF_PAGE_READABLE |
  215. CF_PAGE_WRITABLE |
  216. CF_PAGE_EXEC),
  217. [VM_SHARED] = PAGE_NONE,
  218. [VM_SHARED | VM_READ] = __pgprot(CF_PAGE_VALID |
  219. CF_PAGE_ACCESSED |
  220. CF_PAGE_READABLE),
  221. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  222. [VM_SHARED | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  223. CF_PAGE_ACCESSED |
  224. CF_PAGE_READABLE |
  225. CF_PAGE_SHARED),
  226. [VM_SHARED | VM_EXEC] = __pgprot(CF_PAGE_VALID |
  227. CF_PAGE_ACCESSED |
  228. CF_PAGE_EXEC),
  229. [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(CF_PAGE_VALID |
  230. CF_PAGE_ACCESSED |
  231. CF_PAGE_READABLE |
  232. CF_PAGE_EXEC),
  233. [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(CF_PAGE_VALID |
  234. CF_PAGE_ACCESSED |
  235. CF_PAGE_SHARED |
  236. CF_PAGE_EXEC),
  237. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(CF_PAGE_VALID |
  238. CF_PAGE_ACCESSED |
  239. CF_PAGE_READABLE |
  240. CF_PAGE_SHARED |
  241. CF_PAGE_EXEC)
  242. };
  243. DECLARE_VM_GET_PAGE_PROT