mcfmmu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/bootmem.h>
  16. #include <asm/setup.h>
  17. #include <asm/page.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/mcf_pgalloc.h>
  21. #include <asm/tlbflush.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 zones_size[MAX_NR_ZONES];
  38. enum zone_type zone;
  39. int i;
  40. empty_zero_page = (void *) alloc_bootmem_pages(PAGE_SIZE);
  41. memset((void *) empty_zero_page, 0, PAGE_SIZE);
  42. pg_dir = swapper_pg_dir;
  43. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  44. size = num_pages * sizeof(pte_t);
  45. size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1);
  46. next_pgtable = (unsigned long) alloc_bootmem_pages(size);
  47. bootmem_end = (next_pgtable + size + PAGE_SIZE) & PAGE_MASK;
  48. pg_dir += PAGE_OFFSET >> PGDIR_SHIFT;
  49. address = PAGE_OFFSET;
  50. while (address < (unsigned long)high_memory) {
  51. pg_table = (pte_t *) next_pgtable;
  52. next_pgtable += PTRS_PER_PTE * sizeof(pte_t);
  53. pgd_val(*pg_dir) = (unsigned long) pg_table;
  54. pg_dir++;
  55. /* now change pg_table to kernel virtual addresses */
  56. for (i = 0; i < PTRS_PER_PTE; ++i, ++pg_table) {
  57. pte_t pte = pfn_pte(virt_to_pfn(address), PAGE_INIT);
  58. if (address >= (unsigned long) high_memory)
  59. pte_val(pte) = 0;
  60. set_pte(pg_table, pte);
  61. address += PAGE_SIZE;
  62. }
  63. }
  64. current->mm = NULL;
  65. for (zone = 0; zone < MAX_NR_ZONES; zone++)
  66. zones_size[zone] = 0x0;
  67. zones_size[ZONE_DMA] = num_pages;
  68. free_area_init(zones_size);
  69. }
  70. int cf_tlb_miss(struct pt_regs *regs, int write, int dtlb, int extension_word)
  71. {
  72. unsigned long flags, mmuar, mmutr;
  73. struct mm_struct *mm;
  74. pgd_t *pgd;
  75. pmd_t *pmd;
  76. pte_t *pte;
  77. int asid;
  78. local_irq_save(flags);
  79. mmuar = (dtlb) ? mmu_read(MMUAR) :
  80. regs->pc + (extension_word * sizeof(long));
  81. mm = (!user_mode(regs) && KMAPAREA(mmuar)) ? &init_mm : current->mm;
  82. if (!mm) {
  83. local_irq_restore(flags);
  84. return -1;
  85. }
  86. pgd = pgd_offset(mm, mmuar);
  87. if (pgd_none(*pgd)) {
  88. local_irq_restore(flags);
  89. return -1;
  90. }
  91. pmd = pmd_offset(pgd, mmuar);
  92. if (pmd_none(*pmd)) {
  93. local_irq_restore(flags);
  94. return -1;
  95. }
  96. pte = (KMAPAREA(mmuar)) ? pte_offset_kernel(pmd, mmuar)
  97. : pte_offset_map(pmd, mmuar);
  98. if (pte_none(*pte) || !pte_present(*pte)) {
  99. local_irq_restore(flags);
  100. return -1;
  101. }
  102. if (write) {
  103. if (!pte_write(*pte)) {
  104. local_irq_restore(flags);
  105. return -1;
  106. }
  107. set_pte(pte, pte_mkdirty(*pte));
  108. }
  109. set_pte(pte, pte_mkyoung(*pte));
  110. asid = mm->context & 0xff;
  111. if (!pte_dirty(*pte) && !KMAPAREA(mmuar))
  112. set_pte(pte, pte_wrprotect(*pte));
  113. mmutr = (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) | MMUTR_V;
  114. if ((mmuar < TASK_UNMAPPED_BASE) || (mmuar >= TASK_SIZE))
  115. mmutr |= (pte->pte & CF_PAGE_MMUTR_MASK) >> CF_PAGE_MMUTR_SHIFT;
  116. mmu_write(MMUTR, mmutr);
  117. mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) |
  118. ((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X);
  119. if (dtlb)
  120. mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA);
  121. else
  122. mmu_write(MMUOR, MMUOR_ITLB | MMUOR_ACC | MMUOR_UAA);
  123. local_irq_restore(flags);
  124. return 0;
  125. }
  126. void __init cf_bootmem_alloc(void)
  127. {
  128. unsigned long start_pfn;
  129. unsigned long memstart;
  130. /* _rambase and _ramend will be naturally page aligned */
  131. m68k_memory[0].addr = _rambase;
  132. m68k_memory[0].size = _ramend - _rambase;
  133. /* compute total pages in system */
  134. num_pages = PFN_DOWN(_ramend - _rambase);
  135. /* page numbers */
  136. memstart = PAGE_ALIGN(_ramstart);
  137. min_low_pfn = PFN_DOWN(_rambase);
  138. start_pfn = PFN_DOWN(memstart);
  139. max_pfn = max_low_pfn = PFN_DOWN(_ramend);
  140. high_memory = (void *)_ramend;
  141. m68k_virt_to_node_shift = fls(_ramend - 1) - 6;
  142. module_fixup(NULL, __start_fixup, __stop_fixup);
  143. /* setup bootmem data */
  144. m68k_setup_node(0);
  145. memstart += init_bootmem_node(NODE_DATA(0), start_pfn,
  146. min_low_pfn, max_low_pfn);
  147. free_bootmem_node(NODE_DATA(0), memstart, _ramend - memstart);
  148. }
  149. /*
  150. * Initialize the context management stuff.
  151. * The following was taken from arch/ppc/mmu_context.c
  152. */
  153. void __init mmu_context_init(void)
  154. {
  155. /*
  156. * Some processors have too few contexts to reserve one for
  157. * init_mm, and require using context 0 for a normal task.
  158. * Other processors reserve the use of context zero for the kernel.
  159. * This code assumes FIRST_CONTEXT < 32.
  160. */
  161. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  162. next_mmu_context = FIRST_CONTEXT;
  163. atomic_set(&nr_free_contexts, LAST_CONTEXT - FIRST_CONTEXT + 1);
  164. }
  165. /*
  166. * Steal a context from a task that has one at the moment.
  167. * This is only used on 8xx and 4xx and we presently assume that
  168. * they don't do SMP. If they do then thicfpgalloc.hs will have to check
  169. * whether the MM we steal is in use.
  170. * We also assume that this is only used on systems that don't
  171. * use an MMU hash table - this is true for 8xx and 4xx.
  172. * This isn't an LRU system, it just frees up each context in
  173. * turn (sort-of pseudo-random replacement :). This would be the
  174. * place to implement an LRU scheme if anyone was motivated to do it.
  175. * -- paulus
  176. */
  177. void steal_context(void)
  178. {
  179. struct mm_struct *mm;
  180. /*
  181. * free up context `next_mmu_context'
  182. * if we shouldn't free context 0, don't...
  183. */
  184. if (next_mmu_context < FIRST_CONTEXT)
  185. next_mmu_context = FIRST_CONTEXT;
  186. mm = context_mm[next_mmu_context];
  187. flush_tlb_mm(mm);
  188. destroy_context(mm);
  189. }