mcfmmu.c 5.8 KB

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