blob: 2535828aa84bd0d174b3346c88b826572fe5d50f [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
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
Paul Mackerras14cf11a2005-09-26 16:04:21 +100013#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/types.h>
18#include <linux/mm.h>
19#include <linux/spinlock.h>
20#include <linux/idr.h>
Alexander Grafe85a4712009-11-02 12:02:30 +000021#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100023
24#include <asm/mmu_context.h>
25
26static DEFINE_SPINLOCK(mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000027static DEFINE_IDA(mmu_context_ida);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100028
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +000029/*
30 * The proto-VSID space has 2^35 - 1 segments available for user mappings.
31 * Each segment contains 2^28 bytes. Each context maps 2^44 bytes,
32 * so we can support 2^19-1 contexts (19 == 35 + 28 - 44).
33 */
34#define NO_CONTEXT 0
35#define MAX_CONTEXT ((1UL << 19) - 1)
36
Alexander Grafe85a4712009-11-02 12:02:30 +000037int __init_new_context(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100038{
39 int index;
40 int err;
41
42again:
Anton Blanchard7317ac82010-02-07 12:30:12 +000043 if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044 return -ENOMEM;
45
46 spin_lock(&mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000047 err = ida_get_new_above(&mmu_context_ida, 1, &index);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048 spin_unlock(&mmu_context_lock);
49
50 if (err == -EAGAIN)
51 goto again;
52 else if (err)
53 return err;
54
55 if (index > MAX_CONTEXT) {
Sonny Raof86c97472006-06-27 08:46:09 -040056 spin_lock(&mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000057 ida_remove(&mmu_context_ida, index);
Sonny Raof86c97472006-06-27 08:46:09 -040058 spin_unlock(&mmu_context_lock);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100059 return -ENOMEM;
60 }
61
Alexander Grafe85a4712009-11-02 12:02:30 +000062 return index;
63}
64EXPORT_SYMBOL_GPL(__init_new_context);
65
66int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
67{
68 int index;
69
70 index = __init_new_context();
71 if (index < 0)
72 return index;
73
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100074 /* The old code would re-promote on fork, we don't do that
75 * when using slices as it could cause problem promoting slices
76 * that have been forced down to 4K
77 */
Stephen Rothwelle8ff0642007-08-15 16:51:18 +100078 if (slice_mm_new_context(mm))
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100079 slice_set_user_psize(mm, mmu_virtual_psize);
David Gibsond28513b2009-11-26 18:56:04 +000080 subpage_prot_init_new_context(mm);
Stephen Rothwell9dfe5c532007-08-15 16:33:55 +100081 mm->context.id = index;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100082
83 return 0;
84}
85
Alexander Grafe85a4712009-11-02 12:02:30 +000086void __destroy_context(int context_id)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100087{
88 spin_lock(&mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000089 ida_remove(&mmu_context_ida, context_id);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100090 spin_unlock(&mmu_context_lock);
Alexander Grafe85a4712009-11-02 12:02:30 +000091}
92EXPORT_SYMBOL_GPL(__destroy_context);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100093
Alexander Grafe85a4712009-11-02 12:02:30 +000094void destroy_context(struct mm_struct *mm)
95{
96 __destroy_context(mm->context.id);
David Gibsond28513b2009-11-26 18:56:04 +000097 subpage_prot_free(mm);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100098 mm->context.id = NO_CONTEXT;
99}