mmu_context_book3s64.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "icswx.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. * We do switch_slb() early in fork, even before we setup the
  79. * mm->context.addr_limit. Default to max task size so that we copy the
  80. * default values to paca which will help us to handle slb miss early.
  81. */
  82. mm->context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
  83. /*
  84. * The old code would re-promote on fork, we don't do that when using
  85. * slices as it could cause problem promoting slices that have been
  86. * forced down to 4K.
  87. *
  88. * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check
  89. * explicitly against context.id == 0. This ensures that we properly
  90. * initialize context slice details for newly allocated mm's (which will
  91. * have id == 0) and don't alter context slice inherited via fork (which
  92. * will have id != 0).
  93. *
  94. * We should not be calling init_new_context() on init_mm. Hence a
  95. * check against 0 is OK.
  96. */
  97. if (mm->context.id == 0)
  98. slice_set_user_psize(mm, mmu_virtual_psize);
  99. subpage_prot_init_new_context(mm);
  100. return index;
  101. }
  102. static int radix__init_new_context(struct mm_struct *mm)
  103. {
  104. unsigned long rts_field;
  105. int index;
  106. index = alloc_context_id(1, PRTB_ENTRIES - 1);
  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. mm->context.npu_context = NULL;
  115. return index;
  116. }
  117. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  118. {
  119. int index;
  120. if (radix_enabled())
  121. index = radix__init_new_context(mm);
  122. else
  123. index = hash__init_new_context(mm);
  124. if (index < 0)
  125. return index;
  126. mm->context.id = index;
  127. #ifdef CONFIG_PPC_ICSWX
  128. mm->context.cop_lockp = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
  129. if (!mm->context.cop_lockp) {
  130. __destroy_context(index);
  131. subpage_prot_free(mm);
  132. mm->context.id = MMU_NO_CONTEXT;
  133. return -ENOMEM;
  134. }
  135. spin_lock_init(mm->context.cop_lockp);
  136. #endif /* CONFIG_PPC_ICSWX */
  137. #ifdef CONFIG_PPC_64K_PAGES
  138. mm->context.pte_frag = NULL;
  139. #endif
  140. #ifdef CONFIG_SPAPR_TCE_IOMMU
  141. mm_iommu_init(mm);
  142. #endif
  143. return 0;
  144. }
  145. void __destroy_context(int context_id)
  146. {
  147. spin_lock(&mmu_context_lock);
  148. ida_remove(&mmu_context_ida, context_id);
  149. spin_unlock(&mmu_context_lock);
  150. }
  151. EXPORT_SYMBOL_GPL(__destroy_context);
  152. #ifdef CONFIG_PPC_64K_PAGES
  153. static void destroy_pagetable_page(struct mm_struct *mm)
  154. {
  155. int count;
  156. void *pte_frag;
  157. struct page *page;
  158. pte_frag = mm->context.pte_frag;
  159. if (!pte_frag)
  160. return;
  161. page = virt_to_page(pte_frag);
  162. /* drop all the pending references */
  163. count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT;
  164. /* We allow PTE_FRAG_NR fragments from a PTE page */
  165. if (page_ref_sub_and_test(page, PTE_FRAG_NR - count)) {
  166. pgtable_page_dtor(page);
  167. free_hot_cold_page(page, 0);
  168. }
  169. }
  170. #else
  171. static inline void destroy_pagetable_page(struct mm_struct *mm)
  172. {
  173. return;
  174. }
  175. #endif
  176. void destroy_context(struct mm_struct *mm)
  177. {
  178. #ifdef CONFIG_SPAPR_TCE_IOMMU
  179. WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list));
  180. #endif
  181. #ifdef CONFIG_PPC_ICSWX
  182. drop_cop(mm->context.acop, mm);
  183. kfree(mm->context.cop_lockp);
  184. mm->context.cop_lockp = NULL;
  185. #endif /* CONFIG_PPC_ICSWX */
  186. if (radix_enabled())
  187. process_tb[mm->context.id].prtb1 = 0;
  188. else
  189. subpage_prot_free(mm);
  190. destroy_pagetable_page(mm);
  191. __destroy_context(mm->context.id);
  192. mm->context.id = MMU_NO_CONTEXT;
  193. }
  194. #ifdef CONFIG_PPC_RADIX_MMU
  195. void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
  196. {
  197. asm volatile("isync": : :"memory");
  198. mtspr(SPRN_PID, next->context.id);
  199. asm volatile("isync \n"
  200. PPC_SLBIA(0x7)
  201. : : :"memory");
  202. }
  203. #endif