drm/virtio: Add drm_panic support
[drm/drm-misc.git] / arch / powerpc / mm / book3s32 / mmu_context.c
blob1922f9a6b05850a76014a7528a967472b645c54b
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 substantially follows the
5 * architecture specification. This includes the 6xx, 7xx, 7xxx,
6 * and 8260 implementations but excludes the 8xx and 4xx.
7 * -- paulus
9 * Derived from arch/ppc/mm/init.c:
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
12 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
13 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
14 * Copyright (C) 1996 Paul Mackerras
16 * Derived from "arch/i386/mm/init.c"
17 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
20 #include <linux/mm.h>
21 #include <linux/init.h>
22 #include <linux/export.h>
24 #include <asm/mmu_context.h>
27 * Room for two PTE pointers, usually the kernel and current user pointers
28 * to their respective root page table.
30 void *abatron_pteptrs[2];
33 * On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs
34 * (virtual segment identifiers) for each context. Although the
35 * hardware supports 24-bit VSIDs, and thus >1 million contexts,
36 * we only use 32,768 of them. That is ample, since there can be
37 * at most around 30,000 tasks in the system anyway, and it means
38 * that we can use a bitmap to indicate which contexts are in use.
39 * Using a bitmap means that we entirely avoid all of the problems
40 * that we used to have when the context number overflowed,
41 * particularly on SMP systems.
42 * -- paulus.
44 #define NO_CONTEXT ((unsigned long) -1)
45 #define LAST_CONTEXT 32767
46 #define FIRST_CONTEXT 1
48 static unsigned long next_mmu_context;
49 static unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];
51 unsigned long __init_new_context(void)
53 unsigned long ctx = next_mmu_context;
55 while (test_and_set_bit(ctx, context_map)) {
56 ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
57 if (ctx > LAST_CONTEXT)
58 ctx = 0;
60 next_mmu_context = (ctx + 1) & LAST_CONTEXT;
62 return ctx;
64 EXPORT_SYMBOL_GPL(__init_new_context);
67 * Set up the context for a new address space.
69 int init_new_context(struct task_struct *t, struct mm_struct *mm)
71 mm->context.id = __init_new_context();
72 mm->context.sr0 = CTX_TO_VSID(mm->context.id, 0);
74 if (IS_ENABLED(CONFIG_PPC_KUEP))
75 mm->context.sr0 |= SR_NX;
76 if (!kuap_is_disabled())
77 mm->context.sr0 |= SR_KS;
79 return 0;
83 * Free a context ID. Make sure to call this with preempt disabled!
85 void __destroy_context(unsigned long ctx)
87 clear_bit(ctx, context_map);
89 EXPORT_SYMBOL_GPL(__destroy_context);
92 * We're finished using the context for an address space.
94 void destroy_context(struct mm_struct *mm)
96 preempt_disable();
97 if (mm->context.id != NO_CONTEXT) {
98 __destroy_context(mm->context.id);
99 mm->context.id = NO_CONTEXT;
101 preempt_enable();
105 * Initialize the context management stuff.
107 void __init mmu_context_init(void)
109 /* Reserve context 0 for kernel use */
110 context_map[0] = (1 << FIRST_CONTEXT) - 1;
111 next_mmu_context = FIRST_CONTEXT;
114 void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
116 long id = next->context.id;
118 if (id < 0)
119 panic("mm_struct %p has no context ID", next);
121 isync();
123 update_user_segments(next->context.sr0);
125 if (IS_ENABLED(CONFIG_BDI_SWITCH))
126 abatron_pteptrs[1] = next->pgd;
128 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
129 mtspr(SPRN_SDR1, rol32(__pa(next->pgd), 4) & 0xffff01ff);
131 mb(); /* sync */
132 isync();
134 EXPORT_SYMBOL(switch_mmu_context);