mmu_context_book3s64.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_SPINLOCK(mmu_context_lock);
  27. static DEFINE_IDA(mmu_context_ida);
  28. static int alloc_context_id(int min_id, int max_id)
  29. {
  30. int index, err;
  31. again:
  32. if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
  33. return -ENOMEM;
  34. spin_lock(&mmu_context_lock);
  35. err = ida_get_new_above(&mmu_context_ida, min_id, &index);
  36. spin_unlock(&mmu_context_lock);
  37. if (err == -EAGAIN)
  38. goto again;
  39. else if (err)
  40. return err;
  41. if (index > max_id) {
  42. spin_lock(&mmu_context_lock);
  43. ida_remove(&mmu_context_ida, index);
  44. spin_unlock(&mmu_context_lock);
  45. return -ENOMEM;
  46. }
  47. return index;
  48. }
  49. void hash__reserve_context_id(int id)
  50. {
  51. int rc, result = 0;
  52. do {
  53. if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
  54. break;
  55. spin_lock(&mmu_context_lock);
  56. rc = ida_get_new_above(&mmu_context_ida, id, &result);
  57. spin_unlock(&mmu_context_lock);
  58. } while (rc == -EAGAIN);
  59. WARN(result != id, "mmu: Failed to reserve context id %d (rc %d)\n", id, result);
  60. }
  61. int hash__alloc_context_id(void)
  62. {
  63. unsigned long max;
  64. if (mmu_has_feature(MMU_FTR_68_BIT_VA))
  65. max = MAX_USER_CONTEXT;
  66. else
  67. max = MAX_USER_CONTEXT_65BIT_VA;
  68. return alloc_context_id(MIN_USER_CONTEXT, max);
  69. }
  70. EXPORT_SYMBOL_GPL(hash__alloc_context_id);
  71. static int hash__init_new_context(struct mm_struct *mm)
  72. {
  73. int index;
  74. index = hash__alloc_context_id();
  75. if (index < 0)
  76. return index;
  77. /*
  78. * The old code would re-promote on fork, we don't do that when using
  79. * slices as it could cause problem promoting slices that have been
  80. * forced down to 4K.
  81. *
  82. * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check
  83. * explicitly against context.id == 0. This ensures that we properly
  84. * initialize context slice details for newly allocated mm's (which will
  85. * have id == 0) and don't alter context slice inherited via fork (which
  86. * will have id != 0).
  87. *
  88. * We should not be calling init_new_context() on init_mm. Hence a
  89. * check against 0 is OK.
  90. */
  91. if (mm->context.id == 0)
  92. slice_init_new_context_exec(mm);
  93. subpage_prot_init_new_context(mm);
  94. pkey_mm_init(mm);
  95. return index;
  96. }
  97. static int radix__init_new_context(struct mm_struct *mm)
  98. {
  99. unsigned long rts_field;
  100. int index, max_id;
  101. max_id = (1 << mmu_pid_bits) - 1;
  102. index = alloc_context_id(mmu_base_pid, max_id);
  103. if (index < 0)
  104. return index;
  105. /*
  106. * set the process table entry,
  107. */
  108. rts_field = radix__get_tree_size();
  109. process_tb[index].prtb0 = cpu_to_be64(rts_field | __pa(mm->pgd) | RADIX_PGD_INDEX_SIZE);
  110. /*
  111. * Order the above store with subsequent update of the PID
  112. * register (at which point HW can start loading/caching
  113. * the entry) and the corresponding load by the MMU from
  114. * the L2 cache.
  115. */
  116. asm volatile("ptesync;isync" : : : "memory");
  117. mm->context.npu_context = NULL;
  118. return index;
  119. }
  120. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  121. {
  122. int index;
  123. if (radix_enabled())
  124. index = radix__init_new_context(mm);
  125. else
  126. index = hash__init_new_context(mm);
  127. if (index < 0)
  128. return index;
  129. mm->context.id = index;
  130. #ifdef CONFIG_PPC_64K_PAGES
  131. mm->context.pte_frag = NULL;
  132. #endif
  133. #ifdef CONFIG_SPAPR_TCE_IOMMU
  134. mm_iommu_init(mm);
  135. #endif
  136. atomic_set(&mm->context.active_cpus, 0);
  137. atomic_set(&mm->context.copros, 0);
  138. return 0;
  139. }
  140. void __destroy_context(int context_id)
  141. {
  142. spin_lock(&mmu_context_lock);
  143. ida_remove(&mmu_context_ida, context_id);
  144. spin_unlock(&mmu_context_lock);
  145. }
  146. EXPORT_SYMBOL_GPL(__destroy_context);
  147. static void destroy_contexts(mm_context_t *ctx)
  148. {
  149. int index, context_id;
  150. spin_lock(&mmu_context_lock);
  151. for (index = 0; index < ARRAY_SIZE(ctx->extended_id); index++) {
  152. context_id = ctx->extended_id[index];
  153. if (context_id)
  154. ida_remove(&mmu_context_ida, context_id);
  155. }
  156. spin_unlock(&mmu_context_lock);
  157. }
  158. #ifdef CONFIG_PPC_64K_PAGES
  159. static void destroy_pagetable_page(struct mm_struct *mm)
  160. {
  161. int count;
  162. void *pte_frag;
  163. struct page *page;
  164. pte_frag = mm->context.pte_frag;
  165. if (!pte_frag)
  166. return;
  167. page = virt_to_page(pte_frag);
  168. /* drop all the pending references */
  169. count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT;
  170. /* We allow PTE_FRAG_NR fragments from a PTE page */
  171. if (page_ref_sub_and_test(page, PTE_FRAG_NR - count)) {
  172. pgtable_page_dtor(page);
  173. free_unref_page(page);
  174. }
  175. }
  176. #else
  177. static inline void destroy_pagetable_page(struct mm_struct *mm)
  178. {
  179. return;
  180. }
  181. #endif
  182. void destroy_context(struct mm_struct *mm)
  183. {
  184. #ifdef CONFIG_SPAPR_TCE_IOMMU
  185. WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list));
  186. #endif
  187. if (radix_enabled())
  188. WARN_ON(process_tb[mm->context.id].prtb0 != 0);
  189. else
  190. subpage_prot_free(mm);
  191. destroy_pagetable_page(mm);
  192. destroy_contexts(&mm->context);
  193. mm->context.id = MMU_NO_CONTEXT;
  194. }
  195. void arch_exit_mmap(struct mm_struct *mm)
  196. {
  197. if (radix_enabled()) {
  198. /*
  199. * Radix doesn't have a valid bit in the process table
  200. * entries. However we know that at least P9 implementation
  201. * will avoid caching an entry with an invalid RTS field,
  202. * and 0 is invalid. So this will do.
  203. *
  204. * This runs before the "fullmm" tlb flush in exit_mmap,
  205. * which does a RIC=2 tlbie to clear the process table
  206. * entry. See the "fullmm" comments in tlb-radix.c.
  207. *
  208. * No barrier required here after the store because
  209. * this process will do the invalidate, which starts with
  210. * ptesync.
  211. */
  212. process_tb[mm->context.id].prtb0 = 0;
  213. }
  214. }
  215. #ifdef CONFIG_PPC_RADIX_MMU
  216. void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
  217. {
  218. if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
  219. isync();
  220. mtspr(SPRN_PID, next->context.id);
  221. isync();
  222. asm volatile(PPC_INVALIDATE_ERAT : : :"memory");
  223. } else {
  224. mtspr(SPRN_PID, next->context.id);
  225. isync();
  226. }
  227. }
  228. #endif