1
0

mmu_context.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __M68K_MMU_CONTEXT_H
  3. #define __M68K_MMU_CONTEXT_H
  4. #include <asm-generic/mm_hooks.h>
  5. #include <linux/mm_types.h>
  6. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  7. {
  8. }
  9. #ifdef CONFIG_MMU
  10. #if defined(CONFIG_COLDFIRE)
  11. #include <asm/atomic.h>
  12. #include <asm/bitops.h>
  13. #include <asm/mcfmmu.h>
  14. #include <asm/mmu.h>
  15. #define NO_CONTEXT 256
  16. #define LAST_CONTEXT 255
  17. #define FIRST_CONTEXT 1
  18. extern unsigned long context_map[];
  19. extern mm_context_t next_mmu_context;
  20. extern atomic_t nr_free_contexts;
  21. extern struct mm_struct *context_mm[LAST_CONTEXT+1];
  22. extern void steal_context(void);
  23. static inline void get_mmu_context(struct mm_struct *mm)
  24. {
  25. mm_context_t ctx;
  26. if (mm->context != NO_CONTEXT)
  27. return;
  28. while (atomic_dec_and_test_lt(&nr_free_contexts)) {
  29. atomic_inc(&nr_free_contexts);
  30. steal_context();
  31. }
  32. ctx = next_mmu_context;
  33. while (test_and_set_bit(ctx, context_map)) {
  34. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  35. if (ctx > LAST_CONTEXT)
  36. ctx = 0;
  37. }
  38. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  39. mm->context = ctx;
  40. context_mm[ctx] = mm;
  41. }
  42. /*
  43. * Set up the context for a new address space.
  44. */
  45. #define init_new_context(tsk, mm) (((mm)->context = NO_CONTEXT), 0)
  46. /*
  47. * We're finished using the context for an address space.
  48. */
  49. static inline void destroy_context(struct mm_struct *mm)
  50. {
  51. if (mm->context != NO_CONTEXT) {
  52. clear_bit(mm->context, context_map);
  53. mm->context = NO_CONTEXT;
  54. atomic_inc(&nr_free_contexts);
  55. }
  56. }
  57. static inline void set_context(mm_context_t context, pgd_t *pgd)
  58. {
  59. __asm__ __volatile__ ("movec %0,%%asid" : : "d" (context));
  60. }
  61. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  62. struct task_struct *tsk)
  63. {
  64. get_mmu_context(tsk->mm);
  65. set_context(tsk->mm->context, next->pgd);
  66. }
  67. /*
  68. * After we have set current->mm to a new value, this activates
  69. * the context for the new mm so we see the new mappings.
  70. */
  71. static inline void activate_mm(struct mm_struct *active_mm,
  72. struct mm_struct *mm)
  73. {
  74. get_mmu_context(mm);
  75. set_context(mm->context, mm->pgd);
  76. }
  77. #define deactivate_mm(tsk, mm) do { } while (0)
  78. #define prepare_arch_switch(next) load_ksp_mmu(next)
  79. static inline void load_ksp_mmu(struct task_struct *task)
  80. {
  81. unsigned long flags;
  82. struct mm_struct *mm;
  83. int asid;
  84. pgd_t *pgd;
  85. pmd_t *pmd;
  86. pte_t *pte;
  87. unsigned long mmuar;
  88. local_irq_save(flags);
  89. mmuar = task->thread.ksp;
  90. /* Search for a valid TLB entry, if one is found, don't remap */
  91. mmu_write(MMUAR, mmuar);
  92. mmu_write(MMUOR, MMUOR_STLB | MMUOR_ADR);
  93. if (mmu_read(MMUSR) & MMUSR_HIT)
  94. goto end;
  95. if (mmuar >= PAGE_OFFSET) {
  96. mm = &init_mm;
  97. } else {
  98. pr_info("load_ksp_mmu: non-kernel mm found: 0x%p\n", task->mm);
  99. mm = task->mm;
  100. }
  101. if (!mm)
  102. goto bug;
  103. pgd = pgd_offset(mm, mmuar);
  104. if (pgd_none(*pgd))
  105. goto bug;
  106. pmd = pmd_offset(pgd, mmuar);
  107. if (pmd_none(*pmd))
  108. goto bug;
  109. pte = (mmuar >= PAGE_OFFSET) ? pte_offset_kernel(pmd, mmuar)
  110. : pte_offset_map(pmd, mmuar);
  111. if (pte_none(*pte) || !pte_present(*pte))
  112. goto bug;
  113. set_pte(pte, pte_mkyoung(*pte));
  114. asid = mm->context & 0xff;
  115. if (!pte_dirty(*pte) && mmuar <= PAGE_OFFSET)
  116. set_pte(pte, pte_wrprotect(*pte));
  117. mmu_write(MMUTR, (mmuar & PAGE_MASK) | (asid << MMUTR_IDN) |
  118. (((int)(pte->pte) & (int)CF_PAGE_MMUTR_MASK)
  119. >> CF_PAGE_MMUTR_SHIFT) | MMUTR_V);
  120. mmu_write(MMUDR, (pte_val(*pte) & PAGE_MASK) |
  121. ((pte->pte) & CF_PAGE_MMUDR_MASK) | MMUDR_SZ_8KB | MMUDR_X);
  122. mmu_write(MMUOR, MMUOR_ACC | MMUOR_UAA);
  123. goto end;
  124. bug:
  125. pr_info("ksp load failed: mm=0x%p ksp=0x08%lx\n", mm, mmuar);
  126. end:
  127. local_irq_restore(flags);
  128. }
  129. #elif defined(CONFIG_SUN3)
  130. #include <asm/sun3mmu.h>
  131. #include <linux/sched.h>
  132. extern unsigned long get_free_context(struct mm_struct *mm);
  133. extern void clear_context(unsigned long context);
  134. /* set the context for a new task to unmapped */
  135. static inline int init_new_context(struct task_struct *tsk,
  136. struct mm_struct *mm)
  137. {
  138. mm->context = SUN3_INVALID_CONTEXT;
  139. return 0;
  140. }
  141. /* find the context given to this process, and if it hasn't already
  142. got one, go get one for it. */
  143. static inline void get_mmu_context(struct mm_struct *mm)
  144. {
  145. if (mm->context == SUN3_INVALID_CONTEXT)
  146. mm->context = get_free_context(mm);
  147. }
  148. /* flush context if allocated... */
  149. static inline void destroy_context(struct mm_struct *mm)
  150. {
  151. if (mm->context != SUN3_INVALID_CONTEXT)
  152. clear_context(mm->context);
  153. }
  154. static inline void activate_context(struct mm_struct *mm)
  155. {
  156. get_mmu_context(mm);
  157. sun3_put_context(mm->context);
  158. }
  159. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  160. struct task_struct *tsk)
  161. {
  162. activate_context(tsk->mm);
  163. }
  164. #define deactivate_mm(tsk, mm) do { } while (0)
  165. static inline void activate_mm(struct mm_struct *prev_mm,
  166. struct mm_struct *next_mm)
  167. {
  168. activate_context(next_mm);
  169. }
  170. #else
  171. #include <asm/setup.h>
  172. #include <asm/page.h>
  173. #include <asm/pgalloc.h>
  174. static inline int init_new_context(struct task_struct *tsk,
  175. struct mm_struct *mm)
  176. {
  177. mm->context = virt_to_phys(mm->pgd);
  178. return 0;
  179. }
  180. #define destroy_context(mm) do { } while(0)
  181. static inline void switch_mm_0230(struct mm_struct *mm)
  182. {
  183. unsigned long crp[2] = {
  184. 0x80000000 | _PAGE_TABLE, mm->context
  185. };
  186. unsigned long tmp;
  187. asm volatile (".chip 68030");
  188. /* flush MC68030/MC68020 caches (they are virtually addressed) */
  189. asm volatile (
  190. "movec %%cacr,%0;"
  191. "orw %1,%0; "
  192. "movec %0,%%cacr"
  193. : "=d" (tmp) : "di" (FLUSH_I_AND_D));
  194. /* Switch the root pointer. For a 030-only kernel,
  195. * avoid flushing the whole ATC, we only need to
  196. * flush the user entries. The 68851 does this by
  197. * itself. Avoid a runtime check here.
  198. */
  199. asm volatile (
  200. #ifdef CPU_M68030_ONLY
  201. "pmovefd %0,%%crp; "
  202. "pflush #0,#4"
  203. #else
  204. "pmove %0,%%crp"
  205. #endif
  206. : : "m" (crp[0]));
  207. asm volatile (".chip 68k");
  208. }
  209. static inline void switch_mm_0460(struct mm_struct *mm)
  210. {
  211. asm volatile (".chip 68040");
  212. /* flush address translation cache (user entries) */
  213. asm volatile ("pflushan");
  214. /* switch the root pointer */
  215. asm volatile ("movec %0,%%urp" : : "r" (mm->context));
  216. if (CPU_IS_060) {
  217. unsigned long tmp;
  218. /* clear user entries in the branch cache */
  219. asm volatile (
  220. "movec %%cacr,%0; "
  221. "orl %1,%0; "
  222. "movec %0,%%cacr"
  223. : "=d" (tmp): "di" (0x00200000));
  224. }
  225. asm volatile (".chip 68k");
  226. }
  227. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
  228. {
  229. if (prev != next) {
  230. if (CPU_IS_020_OR_030)
  231. switch_mm_0230(next);
  232. else
  233. switch_mm_0460(next);
  234. }
  235. }
  236. #define deactivate_mm(tsk,mm) do { } while (0)
  237. static inline void activate_mm(struct mm_struct *prev_mm,
  238. struct mm_struct *next_mm)
  239. {
  240. next_mm->context = virt_to_phys(next_mm->pgd);
  241. if (CPU_IS_020_OR_030)
  242. switch_mm_0230(next_mm);
  243. else
  244. switch_mm_0460(next_mm);
  245. }
  246. #endif
  247. #else /* !CONFIG_MMU */
  248. static inline int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  249. {
  250. return 0;
  251. }
  252. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
  253. {
  254. }
  255. #define destroy_context(mm) do { } while (0)
  256. #define deactivate_mm(tsk,mm) do { } while (0)
  257. static inline void activate_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm)
  258. {
  259. }
  260. #endif /* CONFIG_MMU */
  261. #endif /* __M68K_MMU_CONTEXT_H */