xref: /OK3568_Linux_fs/kernel/arch/powerpc/mm/book3s32/mmu_context.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * This file contains the routines for handling the MMU on those
4*4882a593Smuzhiyun  * PowerPC implementations where the MMU substantially follows the
5*4882a593Smuzhiyun  * architecture specification.  This includes the 6xx, 7xx, 7xxx,
6*4882a593Smuzhiyun  * and 8260 implementations but excludes the 8xx and 4xx.
7*4882a593Smuzhiyun  *  -- paulus
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *  Derived from arch/ppc/mm/init.c:
10*4882a593Smuzhiyun  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
13*4882a593Smuzhiyun  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
14*4882a593Smuzhiyun  *    Copyright (C) 1996 Paul Mackerras
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  Derived from "arch/i386/mm/init.c"
17*4882a593Smuzhiyun  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/mm.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/export.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <asm/mmu_context.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs
28*4882a593Smuzhiyun  * (virtual segment identifiers) for each context.  Although the
29*4882a593Smuzhiyun  * hardware supports 24-bit VSIDs, and thus >1 million contexts,
30*4882a593Smuzhiyun  * we only use 32,768 of them.  That is ample, since there can be
31*4882a593Smuzhiyun  * at most around 30,000 tasks in the system anyway, and it means
32*4882a593Smuzhiyun  * that we can use a bitmap to indicate which contexts are in use.
33*4882a593Smuzhiyun  * Using a bitmap means that we entirely avoid all of the problems
34*4882a593Smuzhiyun  * that we used to have when the context number overflowed,
35*4882a593Smuzhiyun  * particularly on SMP systems.
36*4882a593Smuzhiyun  *  -- paulus.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define NO_CONTEXT      	((unsigned long) -1)
39*4882a593Smuzhiyun #define LAST_CONTEXT    	32767
40*4882a593Smuzhiyun #define FIRST_CONTEXT    	1
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * This function defines the mapping from contexts to VSIDs (virtual
44*4882a593Smuzhiyun  * segment IDs).  We use a skew on both the context and the high 4 bits
45*4882a593Smuzhiyun  * of the 32-bit virtual address (the "effective segment ID") in order
46*4882a593Smuzhiyun  * to spread out the entries in the MMU hash table.  Note, if this
47*4882a593Smuzhiyun  * function is changed then arch/ppc/mm/hashtable.S will have to be
48*4882a593Smuzhiyun  * changed to correspond.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * CTX_TO_VSID(ctx, va)	(((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \
52*4882a593Smuzhiyun  *				 & 0xffffff)
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static unsigned long next_mmu_context;
56*4882a593Smuzhiyun static unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];
57*4882a593Smuzhiyun 
__init_new_context(void)58*4882a593Smuzhiyun unsigned long __init_new_context(void)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	unsigned long ctx = next_mmu_context;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	while (test_and_set_bit(ctx, context_map)) {
63*4882a593Smuzhiyun 		ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
64*4882a593Smuzhiyun 		if (ctx > LAST_CONTEXT)
65*4882a593Smuzhiyun 			ctx = 0;
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 	next_mmu_context = (ctx + 1) & LAST_CONTEXT;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return ctx;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__init_new_context);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * Set up the context for a new address space.
75*4882a593Smuzhiyun  */
init_new_context(struct task_struct * t,struct mm_struct * mm)76*4882a593Smuzhiyun int init_new_context(struct task_struct *t, struct mm_struct *mm)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	mm->context.id = __init_new_context();
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	return 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun  * Free a context ID. Make sure to call this with preempt disabled!
85*4882a593Smuzhiyun  */
__destroy_context(unsigned long ctx)86*4882a593Smuzhiyun void __destroy_context(unsigned long ctx)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	clear_bit(ctx, context_map);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__destroy_context);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun  * We're finished using the context for an address space.
94*4882a593Smuzhiyun  */
destroy_context(struct mm_struct * mm)95*4882a593Smuzhiyun void destroy_context(struct mm_struct *mm)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	preempt_disable();
98*4882a593Smuzhiyun 	if (mm->context.id != NO_CONTEXT) {
99*4882a593Smuzhiyun 		__destroy_context(mm->context.id);
100*4882a593Smuzhiyun 		mm->context.id = NO_CONTEXT;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	preempt_enable();
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * Initialize the context management stuff.
107*4882a593Smuzhiyun  */
mmu_context_init(void)108*4882a593Smuzhiyun void __init mmu_context_init(void)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	/* Reserve context 0 for kernel use */
111*4882a593Smuzhiyun 	context_map[0] = (1 << FIRST_CONTEXT) - 1;
112*4882a593Smuzhiyun 	next_mmu_context = FIRST_CONTEXT;
113*4882a593Smuzhiyun }
114