blob: b910d37aea1aa921b567b0d3a9054b76d9a22ec7 [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>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100022
23#include <asm/mmu_context.h>
24
25static DEFINE_SPINLOCK(mmu_context_lock);
26static DEFINE_IDR(mmu_context_idr);
27
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +000028/*
29 * The proto-VSID space has 2^35 - 1 segments available for user mappings.
30 * Each segment contains 2^28 bytes. Each context maps 2^44 bytes,
31 * so we can support 2^19-1 contexts (19 == 35 + 28 - 44).
32 */
33#define NO_CONTEXT 0
34#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:
42 if (!idr_pre_get(&mmu_context_idr, GFP_KERNEL))
43 return -ENOMEM;
44
45 spin_lock(&mmu_context_lock);
46 err = idr_get_new_above(&mmu_context_idr, NULL, 1, &index);
47 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);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100056 idr_remove(&mmu_context_idr, 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);
Alexander Grafe85a4712009-11-02 12:02:30 +000088 idr_remove(&mmu_context_idr, 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);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100097 mm->context.id = NO_CONTEXT;
98}