mmu_context_book3s64.c 5.7 KB

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