blob: c5859448a0ab0e9221bc3ed8e23780a7eabca70c [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 */
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +000034#define MAX_CONTEXT ((1UL << 19) - 1)
35
Alexander Grafe85a4712009-11-02 12:02:30 +000036int __init_new_context(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037{
38 int index;
39 int err;
40
41again:
Anton Blanchard7317ac82010-02-07 12:30:12 +000042 if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
Paul Mackerras14cf11a2005-09-26 16:04:21 +100043 return -ENOMEM;
44
45 spin_lock(&mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000046 err = ida_get_new_above(&mmu_context_ida, 1, &index);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047 spin_unlock(&mmu_context_lock);
48
49 if (err == -EAGAIN)
50 goto again;
51 else if (err)
52 return err;
53
54 if (index > MAX_CONTEXT) {
Sonny Raof86c97472006-06-27 08:46:09 -040055 spin_lock(&mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000056 ida_remove(&mmu_context_ida, index);
Sonny Raof86c97472006-06-27 08:46:09 -040057 spin_unlock(&mmu_context_lock);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100058 return -ENOMEM;
59 }
60
Alexander Grafe85a4712009-11-02 12:02:30 +000061 return index;
62}
63EXPORT_SYMBOL_GPL(__init_new_context);
64
65int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
66{
67 int index;
68
69 index = __init_new_context();
70 if (index < 0)
71 return index;
72
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100073 /* The old code would re-promote on fork, we don't do that
74 * when using slices as it could cause problem promoting slices
75 * that have been forced down to 4K
76 */
Stephen Rothwelle8ff0642007-08-15 16:51:18 +100077 if (slice_mm_new_context(mm))
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100078 slice_set_user_psize(mm, mmu_virtual_psize);
David Gibsond28513b2009-11-26 18:56:04 +000079 subpage_prot_init_new_context(mm);
Stephen Rothwell9dfe5c532007-08-15 16:33:55 +100080 mm->context.id = index;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100081
82 return 0;
83}
84
Alexander Grafe85a4712009-11-02 12:02:30 +000085void __destroy_context(int context_id)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100086{
87 spin_lock(&mmu_context_lock);
Anton Blanchard7317ac82010-02-07 12:30:12 +000088 ida_remove(&mmu_context_ida, context_id);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100089 spin_unlock(&mmu_context_lock);
Alexander Grafe85a4712009-11-02 12:02:30 +000090}
91EXPORT_SYMBOL_GPL(__destroy_context);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100092
Alexander Grafe85a4712009-11-02 12:02:30 +000093void destroy_context(struct mm_struct *mm)
94{
95 __destroy_context(mm->context.id);
David Gibsond28513b2009-11-26 18:56:04 +000096 subpage_prot_free(mm);
Michael Ellerman5e8e7b42011-04-12 19:00:04 +000097 mm->context.id = MMU_NO_CONTEXT;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100098}