mmu_context_book3s64.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * MMU context allocation for 64-bit kernels.
  3. *
  4. * Copyright (C) 2004 Anton Blanchard, IBM Corp. <anton@samba.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/mm.h>
  18. #include <linux/pkeys.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/idr.h>
  21. #include <linux/export.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/pgalloc.h>
  26. static DEFINE_IDA(mmu_context_ida);
  27. static int alloc_context_id(int min_id, int max_id)
  28. {
  29. return ida_alloc_range(&mmu_context_ida, min_id, max_id, GFP_KERNEL);
  30. }
  31. void hash__reserve_context_id(int id)
  32. {
  33. int result = ida_alloc_range(&mmu_context_ida, id, id, GFP_KERNEL);
  34. WARN(result != id, "mmu: Failed to reserve context id %d (rc %d)\n", id, result);
  35. }
  36. int hash__alloc_context_id(void)
  37. {
  38. unsigned long max;
  39. if (mmu_has_feature(MMU_FTR_68_BIT_VA))
  40. max = MAX_USER_CONTEXT;
  41. else
  42. max = MAX_USER_CONTEXT_65BIT_VA;
  43. return alloc_context_id(MIN_USER_CONTEXT, max);
  44. }
  45. EXPORT_SYMBOL_GPL(hash__alloc_context_id);
  46. static int hash__init_new_context(struct mm_struct *mm)
  47. {
  48. int index;
  49. index = hash__alloc_context_id();
  50. if (index < 0)
  51. return index;
  52. /*
  53. * The old code would re-promote on fork, we don't do that when using
  54. * slices as it could cause problem promoting slices that have been
  55. * forced down to 4K.
  56. *
  57. * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check
  58. * explicitly against context.id == 0. This ensures that we properly
  59. * initialize context slice details for newly allocated mm's (which will
  60. * have id == 0) and don't alter context slice inherited via fork (which
  61. * will have id != 0).
  62. *
  63. * We should not be calling init_new_context() on init_mm. Hence a
  64. * check against 0 is OK.
  65. */
  66. if (mm->context.id == 0)
  67. slice_init_new_context_exec(mm);
  68. subpage_prot_init_new_context(mm);
  69. pkey_mm_init(mm);
  70. return index;
  71. }
  72. static int radix__init_new_context(struct mm_struct *mm)
  73. {
  74. unsigned long rts_field;
  75. int index, max_id;
  76. max_id = (1 << mmu_pid_bits) - 1;
  77. index = alloc_context_id(mmu_base_pid, max_id);
  78. if (index < 0)
  79. return index;
  80. /*
  81. * set the process table entry,
  82. */
  83. rts_field = radix__get_tree_size();
  84. process_tb[index].prtb0 = cpu_to_be64(rts_field | __pa(mm->pgd) | RADIX_PGD_INDEX_SIZE);
  85. /*
  86. * Order the above store with subsequent update of the PID
  87. * register (at which point HW can start loading/caching
  88. * the entry) and the corresponding load by the MMU from
  89. * the L2 cache.
  90. */
  91. asm volatile("ptesync;isync" : : : "memory");
  92. mm->context.npu_context = NULL;
  93. return index;
  94. }
  95. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  96. {
  97. int index;
  98. if (radix_enabled())
  99. index = radix__init_new_context(mm);
  100. else
  101. index = hash__init_new_context(mm);
  102. if (index < 0)
  103. return index;
  104. mm->context.id = index;
  105. mm->context.pte_frag = NULL;
  106. mm->context.pmd_frag = NULL;
  107. #ifdef CONFIG_SPAPR_TCE_IOMMU
  108. mm_iommu_init(mm);
  109. #endif
  110. atomic_set(&mm->context.active_cpus, 0);
  111. atomic_set(&mm->context.copros, 0);
  112. return 0;
  113. }
  114. void __destroy_context(int context_id)
  115. {
  116. ida_free(&mmu_context_ida, context_id);
  117. }
  118. EXPORT_SYMBOL_GPL(__destroy_context);
  119. static void destroy_contexts(mm_context_t *ctx)
  120. {
  121. int index, context_id;
  122. for (index = 0; index < ARRAY_SIZE(ctx->extended_id); index++) {
  123. context_id = ctx->extended_id[index];
  124. if (context_id)
  125. ida_free(&mmu_context_ida, context_id);
  126. }
  127. }
  128. static void pte_frag_destroy(void *pte_frag)
  129. {
  130. int count;
  131. struct page *page;
  132. page = virt_to_page(pte_frag);
  133. /* drop all the pending references */
  134. count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT;
  135. /* We allow PTE_FRAG_NR fragments from a PTE page */
  136. if (page_ref_sub_and_test(page, PTE_FRAG_NR - count)) {
  137. pgtable_page_dtor(page);
  138. free_unref_page(page);
  139. }
  140. }
  141. static void pmd_frag_destroy(void *pmd_frag)
  142. {
  143. int count;
  144. struct page *page;
  145. page = virt_to_page(pmd_frag);
  146. /* drop all the pending references */
  147. count = ((unsigned long)pmd_frag & ~PAGE_MASK) >> PMD_FRAG_SIZE_SHIFT;
  148. /* We allow PTE_FRAG_NR fragments from a PTE page */
  149. if (page_ref_sub_and_test(page, PMD_FRAG_NR - count)) {
  150. pgtable_pmd_page_dtor(page);
  151. free_unref_page(page);
  152. }
  153. }
  154. static void destroy_pagetable_page(struct mm_struct *mm)
  155. {
  156. void *frag;
  157. frag = mm->context.pte_frag;
  158. if (frag)
  159. pte_frag_destroy(frag);
  160. frag = mm->context.pmd_frag;
  161. if (frag)
  162. pmd_frag_destroy(frag);
  163. return;
  164. }
  165. void destroy_context(struct mm_struct *mm)
  166. {
  167. #ifdef CONFIG_SPAPR_TCE_IOMMU
  168. WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list));
  169. #endif
  170. if (radix_enabled())
  171. WARN_ON(process_tb[mm->context.id].prtb0 != 0);
  172. else
  173. subpage_prot_free(mm);
  174. destroy_pagetable_page(mm);
  175. destroy_contexts(&mm->context);
  176. mm->context.id = MMU_NO_CONTEXT;
  177. }
  178. void arch_exit_mmap(struct mm_struct *mm)
  179. {
  180. if (radix_enabled()) {
  181. /*
  182. * Radix doesn't have a valid bit in the process table
  183. * entries. However we know that at least P9 implementation
  184. * will avoid caching an entry with an invalid RTS field,
  185. * and 0 is invalid. So this will do.
  186. *
  187. * This runs before the "fullmm" tlb flush in exit_mmap,
  188. * which does a RIC=2 tlbie to clear the process table
  189. * entry. See the "fullmm" comments in tlb-radix.c.
  190. *
  191. * No barrier required here after the store because
  192. * this process will do the invalidate, which starts with
  193. * ptesync.
  194. */
  195. process_tb[mm->context.id].prtb0 = 0;
  196. }
  197. }
  198. #ifdef CONFIG_PPC_RADIX_MMU
  199. void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
  200. {
  201. if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
  202. isync();
  203. mtspr(SPRN_PID, next->context.id);
  204. isync();
  205. asm volatile(PPC_INVALIDATE_ERAT : : :"memory");
  206. } else {
  207. mtspr(SPRN_PID, next->context.id);
  208. isync();
  209. }
  210. }
  211. #endif