Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
| 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 Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 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> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/idr.h> |
Alexander Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 21 | #include <linux/module.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 22 | |
| 23 | #include <asm/mmu_context.h> |
| 24 | |
| 25 | static DEFINE_SPINLOCK(mmu_context_lock); |
| 26 | static DEFINE_IDR(mmu_context_idr); |
| 27 | |
Benjamin Herrenschmidt | 5e69661 | 2008-12-18 19:13:24 +0000 | [diff] [blame] | 28 | /* |
| 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 Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 36 | int __init_new_context(void) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 37 | { |
| 38 | int index; |
| 39 | int err; |
| 40 | |
| 41 | again: |
| 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 Rao | f86c9747 | 2006-06-27 08:46:09 -0400 | [diff] [blame] | 55 | spin_lock(&mmu_context_lock); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 56 | idr_remove(&mmu_context_idr, index); |
Sonny Rao | f86c9747 | 2006-06-27 08:46:09 -0400 | [diff] [blame] | 57 | spin_unlock(&mmu_context_lock); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 58 | return -ENOMEM; |
| 59 | } |
| 60 | |
Alexander Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 61 | return index; |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(__init_new_context); |
| 64 | |
| 65 | int 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 Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 73 | /* 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 Rothwell | e8ff064 | 2007-08-15 16:51:18 +1000 | [diff] [blame] | 77 | if (slice_mm_new_context(mm)) |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 78 | slice_set_user_psize(mm, mmu_virtual_psize); |
Stephen Rothwell | 9dfe5c53 | 2007-08-15 16:33:55 +1000 | [diff] [blame] | 79 | mm->context.id = index; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Alexander Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 84 | void __destroy_context(int context_id) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 85 | { |
| 86 | spin_lock(&mmu_context_lock); |
Alexander Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 87 | idr_remove(&mmu_context_idr, context_id); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 88 | spin_unlock(&mmu_context_lock); |
Alexander Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 89 | } |
| 90 | EXPORT_SYMBOL_GPL(__destroy_context); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 91 | |
Alexander Graf | e85a471 | 2009-11-02 12:02:30 +0000 | [diff] [blame^] | 92 | void destroy_context(struct mm_struct *mm) |
| 93 | { |
| 94 | __destroy_context(mm->context.id); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 95 | mm->context.id = NO_CONTEXT; |
| 96 | } |