mmu_context.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains the routines for handling the MMU on those
  4. * PowerPC implementations where the MMU is not using the hash
  5. * table, such as 8xx, 4xx, BookE's etc...
  6. *
  7. * Copyright 2008 Ben Herrenschmidt <[email protected]>
  8. * IBM Corp.
  9. *
  10. * Derived from previous arch/powerpc/mm/mmu_context.c
  11. * and arch/powerpc/include/asm/mmu_context.h
  12. *
  13. * TODO:
  14. *
  15. * - The global context lock will not scale very well
  16. * - The maps should be dynamically allocated to allow for processors
  17. * that support more PID bits at runtime
  18. * - Implement flush_tlb_mm() by making the context stale and picking
  19. * a new one
  20. * - More aggressively clear stale map bits and maybe find some way to
  21. * also clear mm->cpu_vm_mask bits when processes are migrated
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/memblock.h>
  28. #include <linux/notifier.h>
  29. #include <linux/cpu.h>
  30. #include <linux/slab.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/smp.h>
  34. #include <asm/kup.h>
  35. #include <mm/mmu_decl.h>
  36. /*
  37. * Room for two PTE table pointers, usually the kernel and current user
  38. * pointer to their respective root page table (pgdir).
  39. */
  40. void *abatron_pteptrs[2];
  41. /*
  42. * The MPC8xx has only 16 contexts. We rotate through them on each task switch.
  43. * A better way would be to keep track of tasks that own contexts, and implement
  44. * an LRU usage. That way very active tasks don't always have to pay the TLB
  45. * reload overhead. The kernel pages are mapped shared, so the kernel can run on
  46. * behalf of any task that makes a kernel entry. Shared does not mean they are
  47. * not protected, just that the ASID comparison is not performed. -- Dan
  48. *
  49. * The IBM4xx has 256 contexts, so we can just rotate through these as a way of
  50. * "switching" contexts. If the TID of the TLB is zero, the PID/TID comparison
  51. * is disabled, so we can use a TID of zero to represent all kernel pages as
  52. * shared among all contexts. -- Dan
  53. *
  54. * The IBM 47x core supports 16-bit PIDs, thus 65535 contexts. We should
  55. * normally never have to steal though the facility is present if needed.
  56. * -- BenH
  57. */
  58. #define FIRST_CONTEXT 1
  59. #if defined(CONFIG_PPC_8xx)
  60. #define LAST_CONTEXT 16
  61. #elif defined(CONFIG_PPC_47x)
  62. #define LAST_CONTEXT 65535
  63. #else
  64. #define LAST_CONTEXT 255
  65. #endif
  66. static unsigned int next_context, nr_free_contexts;
  67. static unsigned long *context_map;
  68. static unsigned long *stale_map[NR_CPUS];
  69. static struct mm_struct **context_mm;
  70. static DEFINE_RAW_SPINLOCK(context_lock);
  71. #define CTX_MAP_SIZE \
  72. (sizeof(unsigned long) * (LAST_CONTEXT / BITS_PER_LONG + 1))
  73. /* Steal a context from a task that has one at the moment.
  74. *
  75. * This is used when we are running out of available PID numbers
  76. * on the processors.
  77. *
  78. * This isn't an LRU system, it just frees up each context in
  79. * turn (sort-of pseudo-random replacement :). This would be the
  80. * place to implement an LRU scheme if anyone was motivated to do it.
  81. * -- paulus
  82. *
  83. * For context stealing, we use a slightly different approach for
  84. * SMP and UP. Basically, the UP one is simpler and doesn't use
  85. * the stale map as we can just flush the local CPU
  86. * -- benh
  87. */
  88. static unsigned int steal_context_smp(unsigned int id)
  89. {
  90. struct mm_struct *mm;
  91. unsigned int cpu, max, i;
  92. max = LAST_CONTEXT - FIRST_CONTEXT;
  93. /* Attempt to free next_context first and then loop until we manage */
  94. while (max--) {
  95. /* Pick up the victim mm */
  96. mm = context_mm[id];
  97. /* We have a candidate victim, check if it's active, on SMP
  98. * we cannot steal active contexts
  99. */
  100. if (mm->context.active) {
  101. id++;
  102. if (id > LAST_CONTEXT)
  103. id = FIRST_CONTEXT;
  104. continue;
  105. }
  106. /* Mark this mm has having no context anymore */
  107. mm->context.id = MMU_NO_CONTEXT;
  108. /* Mark it stale on all CPUs that used this mm. For threaded
  109. * implementations, we set it on all threads on each core
  110. * represented in the mask. A future implementation will use
  111. * a core map instead but this will do for now.
  112. */
  113. for_each_cpu(cpu, mm_cpumask(mm)) {
  114. for (i = cpu_first_thread_sibling(cpu);
  115. i <= cpu_last_thread_sibling(cpu); i++) {
  116. if (stale_map[i])
  117. __set_bit(id, stale_map[i]);
  118. }
  119. cpu = i - 1;
  120. }
  121. return id;
  122. }
  123. /* This will happen if you have more CPUs than available contexts,
  124. * all we can do here is wait a bit and try again
  125. */
  126. raw_spin_unlock(&context_lock);
  127. cpu_relax();
  128. raw_spin_lock(&context_lock);
  129. /* This will cause the caller to try again */
  130. return MMU_NO_CONTEXT;
  131. }
  132. static unsigned int steal_all_contexts(void)
  133. {
  134. struct mm_struct *mm;
  135. int cpu = smp_processor_id();
  136. unsigned int id;
  137. for (id = FIRST_CONTEXT; id <= LAST_CONTEXT; id++) {
  138. /* Pick up the victim mm */
  139. mm = context_mm[id];
  140. /* Mark this mm as having no context anymore */
  141. mm->context.id = MMU_NO_CONTEXT;
  142. if (id != FIRST_CONTEXT) {
  143. context_mm[id] = NULL;
  144. __clear_bit(id, context_map);
  145. }
  146. if (IS_ENABLED(CONFIG_SMP))
  147. __clear_bit(id, stale_map[cpu]);
  148. }
  149. /* Flush the TLB for all contexts (not to be used on SMP) */
  150. _tlbil_all();
  151. nr_free_contexts = LAST_CONTEXT - FIRST_CONTEXT;
  152. return FIRST_CONTEXT;
  153. }
  154. /* Note that this will also be called on SMP if all other CPUs are
  155. * offlined, which means that it may be called for cpu != 0. For
  156. * this to work, we somewhat assume that CPUs that are onlined
  157. * come up with a fully clean TLB (or are cleaned when offlined)
  158. */
  159. static unsigned int steal_context_up(unsigned int id)
  160. {
  161. struct mm_struct *mm;
  162. int cpu = smp_processor_id();
  163. /* Pick up the victim mm */
  164. mm = context_mm[id];
  165. /* Flush the TLB for that context */
  166. local_flush_tlb_mm(mm);
  167. /* Mark this mm has having no context anymore */
  168. mm->context.id = MMU_NO_CONTEXT;
  169. /* XXX This clear should ultimately be part of local_flush_tlb_mm */
  170. if (IS_ENABLED(CONFIG_SMP))
  171. __clear_bit(id, stale_map[cpu]);
  172. return id;
  173. }
  174. static void set_context(unsigned long id, pgd_t *pgd)
  175. {
  176. if (IS_ENABLED(CONFIG_PPC_8xx)) {
  177. s16 offset = (s16)(__pa(swapper_pg_dir));
  178. /*
  179. * Register M_TWB will contain base address of level 1 table minus the
  180. * lower part of the kernel PGDIR base address, so that all accesses to
  181. * level 1 table are done relative to lower part of kernel PGDIR base
  182. * address.
  183. */
  184. mtspr(SPRN_M_TWB, __pa(pgd) - offset);
  185. /* Update context */
  186. mtspr(SPRN_M_CASID, id - 1);
  187. /* sync */
  188. mb();
  189. } else if (kuap_is_disabled()) {
  190. if (IS_ENABLED(CONFIG_40x))
  191. mb(); /* sync */
  192. mtspr(SPRN_PID, id);
  193. isync();
  194. }
  195. }
  196. void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next,
  197. struct task_struct *tsk)
  198. {
  199. unsigned int id;
  200. unsigned int i, cpu = smp_processor_id();
  201. unsigned long *map;
  202. /* No lockless fast path .. yet */
  203. raw_spin_lock(&context_lock);
  204. if (IS_ENABLED(CONFIG_SMP)) {
  205. /* Mark us active and the previous one not anymore */
  206. next->context.active++;
  207. if (prev) {
  208. WARN_ON(prev->context.active < 1);
  209. prev->context.active--;
  210. }
  211. }
  212. again:
  213. /* If we already have a valid assigned context, skip all that */
  214. id = next->context.id;
  215. if (likely(id != MMU_NO_CONTEXT))
  216. goto ctxt_ok;
  217. /* We really don't have a context, let's try to acquire one */
  218. id = next_context;
  219. if (id > LAST_CONTEXT)
  220. id = FIRST_CONTEXT;
  221. map = context_map;
  222. /* No more free contexts, let's try to steal one */
  223. if (nr_free_contexts == 0) {
  224. if (num_online_cpus() > 1) {
  225. id = steal_context_smp(id);
  226. if (id == MMU_NO_CONTEXT)
  227. goto again;
  228. goto stolen;
  229. }
  230. if (IS_ENABLED(CONFIG_PPC_8xx))
  231. id = steal_all_contexts();
  232. else
  233. id = steal_context_up(id);
  234. goto stolen;
  235. }
  236. nr_free_contexts--;
  237. /* We know there's at least one free context, try to find it */
  238. while (__test_and_set_bit(id, map)) {
  239. id = find_next_zero_bit(map, LAST_CONTEXT+1, id);
  240. if (id > LAST_CONTEXT)
  241. id = FIRST_CONTEXT;
  242. }
  243. stolen:
  244. next_context = id + 1;
  245. context_mm[id] = next;
  246. next->context.id = id;
  247. ctxt_ok:
  248. /* If that context got marked stale on this CPU, then flush the
  249. * local TLB for it and unmark it before we use it
  250. */
  251. if (IS_ENABLED(CONFIG_SMP) && test_bit(id, stale_map[cpu])) {
  252. local_flush_tlb_mm(next);
  253. /* XXX This clear should ultimately be part of local_flush_tlb_mm */
  254. for (i = cpu_first_thread_sibling(cpu);
  255. i <= cpu_last_thread_sibling(cpu); i++) {
  256. if (stale_map[i])
  257. __clear_bit(id, stale_map[i]);
  258. }
  259. }
  260. /* Flick the MMU and release lock */
  261. if (IS_ENABLED(CONFIG_BDI_SWITCH))
  262. abatron_pteptrs[1] = next->pgd;
  263. set_context(id, next->pgd);
  264. #if defined(CONFIG_BOOKE_OR_40x) && defined(CONFIG_PPC_KUAP)
  265. tsk->thread.pid = id;
  266. #endif
  267. raw_spin_unlock(&context_lock);
  268. }
  269. /*
  270. * Set up the context for a new address space.
  271. */
  272. int init_new_context(struct task_struct *t, struct mm_struct *mm)
  273. {
  274. mm->context.id = MMU_NO_CONTEXT;
  275. mm->context.active = 0;
  276. pte_frag_set(&mm->context, NULL);
  277. return 0;
  278. }
  279. /*
  280. * We're finished using the context for an address space.
  281. */
  282. void destroy_context(struct mm_struct *mm)
  283. {
  284. unsigned long flags;
  285. unsigned int id;
  286. if (mm->context.id == MMU_NO_CONTEXT)
  287. return;
  288. WARN_ON(mm->context.active != 0);
  289. raw_spin_lock_irqsave(&context_lock, flags);
  290. id = mm->context.id;
  291. if (id != MMU_NO_CONTEXT) {
  292. __clear_bit(id, context_map);
  293. mm->context.id = MMU_NO_CONTEXT;
  294. context_mm[id] = NULL;
  295. nr_free_contexts++;
  296. }
  297. raw_spin_unlock_irqrestore(&context_lock, flags);
  298. }
  299. static int mmu_ctx_cpu_prepare(unsigned int cpu)
  300. {
  301. /* We don't touch CPU 0 map, it's allocated at aboot and kept
  302. * around forever
  303. */
  304. if (cpu == boot_cpuid)
  305. return 0;
  306. stale_map[cpu] = kzalloc(CTX_MAP_SIZE, GFP_KERNEL);
  307. return 0;
  308. }
  309. static int mmu_ctx_cpu_dead(unsigned int cpu)
  310. {
  311. #ifdef CONFIG_HOTPLUG_CPU
  312. if (cpu == boot_cpuid)
  313. return 0;
  314. kfree(stale_map[cpu]);
  315. stale_map[cpu] = NULL;
  316. /* We also clear the cpu_vm_mask bits of CPUs going away */
  317. clear_tasks_mm_cpumask(cpu);
  318. #endif
  319. return 0;
  320. }
  321. /*
  322. * Initialize the context management stuff.
  323. */
  324. void __init mmu_context_init(void)
  325. {
  326. /* Mark init_mm as being active on all possible CPUs since
  327. * we'll get called with prev == init_mm the first time
  328. * we schedule on a given CPU
  329. */
  330. init_mm.context.active = NR_CPUS;
  331. /*
  332. * Allocate the maps used by context management
  333. */
  334. context_map = memblock_alloc(CTX_MAP_SIZE, SMP_CACHE_BYTES);
  335. if (!context_map)
  336. panic("%s: Failed to allocate %zu bytes\n", __func__,
  337. CTX_MAP_SIZE);
  338. context_mm = memblock_alloc(sizeof(void *) * (LAST_CONTEXT + 1),
  339. SMP_CACHE_BYTES);
  340. if (!context_mm)
  341. panic("%s: Failed to allocate %zu bytes\n", __func__,
  342. sizeof(void *) * (LAST_CONTEXT + 1));
  343. if (IS_ENABLED(CONFIG_SMP)) {
  344. stale_map[boot_cpuid] = memblock_alloc(CTX_MAP_SIZE, SMP_CACHE_BYTES);
  345. if (!stale_map[boot_cpuid])
  346. panic("%s: Failed to allocate %zu bytes\n", __func__,
  347. CTX_MAP_SIZE);
  348. cpuhp_setup_state_nocalls(CPUHP_POWERPC_MMU_CTX_PREPARE,
  349. "powerpc/mmu/ctx:prepare",
  350. mmu_ctx_cpu_prepare, mmu_ctx_cpu_dead);
  351. }
  352. printk(KERN_INFO
  353. "MMU: Allocated %zu bytes of context maps for %d contexts\n",
  354. 2 * CTX_MAP_SIZE + (sizeof(void *) * (LAST_CONTEXT + 1)),
  355. LAST_CONTEXT - FIRST_CONTEXT + 1);
  356. /*
  357. * Some processors have too few contexts to reserve one for
  358. * init_mm, and require using context 0 for a normal task.
  359. * Other processors reserve the use of context zero for the kernel.
  360. * This code assumes FIRST_CONTEXT < 32.
  361. */
  362. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  363. next_context = FIRST_CONTEXT;
  364. nr_free_contexts = LAST_CONTEXT - FIRST_CONTEXT + 1;
  365. }