2 * MMU context allocation for 64-bit kernels.
4 * Copyright (C) 2004 Anton Blanchard, IBM Corp. <anton@samba.org>
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.
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
19 #include <linux/spinlock.h>
20 #include <linux/idr.h>
21 #include <linux/export.h>
22 #include <linux/gfp.h>
23 #include <linux/slab.h>
25 #include <asm/mmu_context.h>
29 static DEFINE_SPINLOCK(mmu_context_lock);
30 static DEFINE_IDA(mmu_context_ida);
32 int __init_new_context(void)
38 if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
41 spin_lock(&mmu_context_lock);
42 err = ida_get_new_above(&mmu_context_ida, 1, &index);
43 spin_unlock(&mmu_context_lock);
50 if (index > MAX_USER_CONTEXT) {
51 spin_lock(&mmu_context_lock);
52 ida_remove(&mmu_context_ida, index);
53 spin_unlock(&mmu_context_lock);
59 EXPORT_SYMBOL_GPL(__init_new_context);
61 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
65 index = __init_new_context();
69 /* The old code would re-promote on fork, we don't do that
70 * when using slices as it could cause problem promoting slices
71 * that have been forced down to 4K
73 if (slice_mm_new_context(mm))
74 slice_set_user_psize(mm, mmu_virtual_psize);
75 subpage_prot_init_new_context(mm);
76 mm->context.id = index;
77 #ifdef CONFIG_PPC_ICSWX
78 mm->context.cop_lockp = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
79 if (!mm->context.cop_lockp) {
80 __destroy_context(index);
81 subpage_prot_free(mm);
82 mm->context.id = MMU_NO_CONTEXT;
85 spin_lock_init(mm->context.cop_lockp);
86 #endif /* CONFIG_PPC_ICSWX */
91 void __destroy_context(int context_id)
93 spin_lock(&mmu_context_lock);
94 ida_remove(&mmu_context_ida, context_id);
95 spin_unlock(&mmu_context_lock);
97 EXPORT_SYMBOL_GPL(__destroy_context);
99 void destroy_context(struct mm_struct *mm)
101 #ifdef CONFIG_PPC_ICSWX
102 drop_cop(mm->context.acop, mm);
103 kfree(mm->context.cop_lockp);
104 mm->context.cop_lockp = NULL;
105 #endif /* CONFIG_PPC_ICSWX */
106 __destroy_context(mm->context.id);
107 subpage_prot_free(mm);
108 mm->context.id = MMU_NO_CONTEXT;