mmu_context.h 7.0 KB

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