mmu_context_hash64.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/mm.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/idr.h>
  20. #include <linux/export.h>
  21. #include <linux/gfp.h>
  22. #include <linux/slab.h>
  23. #include <asm/mmu_context.h>
  24. #include "icswx.h"
  25. static DEFINE_SPINLOCK(mmu_context_lock);
  26. static DEFINE_IDA(mmu_context_ida);
  27. /*
  28. * 256MB segment
  29. * The proto-VSID space has 2^(CONTEX_BITS + USER_ESID_BITS) - 1 segments
  30. * available for user mappings. Each segment contains 2^28 bytes. Each
  31. * context maps 2^46 bytes (64TB) so we can support 2^19-1 contexts
  32. * (19 == 37 + 28 - 46).
  33. */
  34. #define MAX_CONTEXT ((1UL << CONTEXT_BITS) - 1)
  35. int __init_new_context(void)
  36. {
  37. int index;
  38. int err;
  39. again:
  40. if (!ida_pre_get(&mmu_context_ida, GFP_KERNEL))
  41. return -ENOMEM;
  42. spin_lock(&mmu_context_lock);
  43. err = ida_get_new_above(&mmu_context_ida, 1, &index);
  44. spin_unlock(&mmu_context_lock);
  45. if (err == -EAGAIN)
  46. goto again;
  47. else if (err)
  48. return err;
  49. if (index > MAX_CONTEXT) {
  50. spin_lock(&mmu_context_lock);
  51. ida_remove(&mmu_context_ida, index);
  52. spin_unlock(&mmu_context_lock);
  53. return -ENOMEM;
  54. }
  55. return index;
  56. }
  57. EXPORT_SYMBOL_GPL(__init_new_context);
  58. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  59. {
  60. int index;
  61. index = __init_new_context();
  62. if (index < 0)
  63. return index;
  64. /* The old code would re-promote on fork, we don't do that
  65. * when using slices as it could cause problem promoting slices
  66. * that have been forced down to 4K
  67. */
  68. if (slice_mm_new_context(mm))
  69. slice_set_user_psize(mm, mmu_virtual_psize);
  70. subpage_prot_init_new_context(mm);
  71. mm->context.id = index;
  72. #ifdef CONFIG_PPC_ICSWX
  73. mm->context.cop_lockp = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
  74. if (!mm->context.cop_lockp) {
  75. __destroy_context(index);
  76. subpage_prot_free(mm);
  77. mm->context.id = MMU_NO_CONTEXT;
  78. return -ENOMEM;
  79. }
  80. spin_lock_init(mm->context.cop_lockp);
  81. #endif /* CONFIG_PPC_ICSWX */
  82. return 0;
  83. }
  84. void __destroy_context(int context_id)
  85. {
  86. spin_lock(&mmu_context_lock);
  87. ida_remove(&mmu_context_ida, context_id);
  88. spin_unlock(&mmu_context_lock);
  89. }
  90. EXPORT_SYMBOL_GPL(__destroy_context);
  91. void destroy_context(struct mm_struct *mm)
  92. {
  93. #ifdef CONFIG_PPC_ICSWX
  94. drop_cop(mm->context.acop, mm);
  95. kfree(mm->context.cop_lockp);
  96. mm->context.cop_lockp = NULL;
  97. #endif /* CONFIG_PPC_ICSWX */
  98. __destroy_context(mm->context.id);
  99. subpage_prot_free(mm);
  100. mm->context.id = MMU_NO_CONTEXT;
  101. }