context.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * SPU file system -- SPU context management
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/atomic.h>
  26. #include <linux/sched.h>
  27. #include <asm/spu.h>
  28. #include <asm/spu_csa.h>
  29. #include "spufs.h"
  30. #include "sputrace.h"
  31. atomic_t nr_spu_contexts = ATOMIC_INIT(0);
  32. struct spu_context *alloc_spu_context(struct spu_gang *gang)
  33. {
  34. struct spu_context *ctx;
  35. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  36. if (!ctx)
  37. goto out;
  38. /* Binding to physical processor deferred
  39. * until spu_activate().
  40. */
  41. if (spu_init_csa(&ctx->csa))
  42. goto out_free;
  43. spin_lock_init(&ctx->mmio_lock);
  44. mutex_init(&ctx->mapping_lock);
  45. kref_init(&ctx->kref);
  46. mutex_init(&ctx->state_mutex);
  47. mutex_init(&ctx->run_mutex);
  48. init_waitqueue_head(&ctx->ibox_wq);
  49. init_waitqueue_head(&ctx->wbox_wq);
  50. init_waitqueue_head(&ctx->stop_wq);
  51. init_waitqueue_head(&ctx->mfc_wq);
  52. init_waitqueue_head(&ctx->run_wq);
  53. ctx->state = SPU_STATE_SAVED;
  54. ctx->ops = &spu_backing_ops;
  55. ctx->owner = get_task_mm(current);
  56. INIT_LIST_HEAD(&ctx->rq);
  57. INIT_LIST_HEAD(&ctx->aff_list);
  58. if (gang)
  59. spu_gang_add_ctx(gang, ctx);
  60. __spu_update_sched_info(ctx);
  61. spu_set_timeslice(ctx);
  62. ctx->stats.util_state = SPU_UTIL_IDLE_LOADED;
  63. ctx->stats.tstamp = ktime_get_ns();
  64. atomic_inc(&nr_spu_contexts);
  65. goto out;
  66. out_free:
  67. kfree(ctx);
  68. ctx = NULL;
  69. out:
  70. return ctx;
  71. }
  72. void destroy_spu_context(struct kref *kref)
  73. {
  74. struct spu_context *ctx;
  75. ctx = container_of(kref, struct spu_context, kref);
  76. spu_context_nospu_trace(destroy_spu_context__enter, ctx);
  77. mutex_lock(&ctx->state_mutex);
  78. spu_deactivate(ctx);
  79. mutex_unlock(&ctx->state_mutex);
  80. spu_fini_csa(&ctx->csa);
  81. if (ctx->gang)
  82. spu_gang_remove_ctx(ctx->gang, ctx);
  83. if (ctx->prof_priv_kref)
  84. kref_put(ctx->prof_priv_kref, ctx->prof_priv_release);
  85. BUG_ON(!list_empty(&ctx->rq));
  86. atomic_dec(&nr_spu_contexts);
  87. kfree(ctx->switch_log);
  88. kfree(ctx);
  89. }
  90. struct spu_context * get_spu_context(struct spu_context *ctx)
  91. {
  92. kref_get(&ctx->kref);
  93. return ctx;
  94. }
  95. int put_spu_context(struct spu_context *ctx)
  96. {
  97. return kref_put(&ctx->kref, &destroy_spu_context);
  98. }
  99. /* give up the mm reference when the context is about to be destroyed */
  100. void spu_forget(struct spu_context *ctx)
  101. {
  102. struct mm_struct *mm;
  103. /*
  104. * This is basically an open-coded spu_acquire_saved, except that
  105. * we don't acquire the state mutex interruptible, and we don't
  106. * want this context to be rescheduled on release.
  107. */
  108. mutex_lock(&ctx->state_mutex);
  109. if (ctx->state != SPU_STATE_SAVED)
  110. spu_deactivate(ctx);
  111. mm = ctx->owner;
  112. ctx->owner = NULL;
  113. mmput(mm);
  114. spu_release(ctx);
  115. }
  116. void spu_unmap_mappings(struct spu_context *ctx)
  117. {
  118. mutex_lock(&ctx->mapping_lock);
  119. if (ctx->local_store)
  120. unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1);
  121. if (ctx->mfc)
  122. unmap_mapping_range(ctx->mfc, 0, SPUFS_MFC_MAP_SIZE, 1);
  123. if (ctx->cntl)
  124. unmap_mapping_range(ctx->cntl, 0, SPUFS_CNTL_MAP_SIZE, 1);
  125. if (ctx->signal1)
  126. unmap_mapping_range(ctx->signal1, 0, SPUFS_SIGNAL_MAP_SIZE, 1);
  127. if (ctx->signal2)
  128. unmap_mapping_range(ctx->signal2, 0, SPUFS_SIGNAL_MAP_SIZE, 1);
  129. if (ctx->mss)
  130. unmap_mapping_range(ctx->mss, 0, SPUFS_MSS_MAP_SIZE, 1);
  131. if (ctx->psmap)
  132. unmap_mapping_range(ctx->psmap, 0, SPUFS_PS_MAP_SIZE, 1);
  133. mutex_unlock(&ctx->mapping_lock);
  134. }
  135. /**
  136. * spu_acquire_saved - lock spu contex and make sure it is in saved state
  137. * @ctx: spu contex to lock
  138. */
  139. int spu_acquire_saved(struct spu_context *ctx)
  140. {
  141. int ret;
  142. spu_context_nospu_trace(spu_acquire_saved__enter, ctx);
  143. ret = spu_acquire(ctx);
  144. if (ret)
  145. return ret;
  146. if (ctx->state != SPU_STATE_SAVED) {
  147. set_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags);
  148. spu_deactivate(ctx);
  149. }
  150. return 0;
  151. }
  152. /**
  153. * spu_release_saved - unlock spu context and return it to the runqueue
  154. * @ctx: context to unlock
  155. */
  156. void spu_release_saved(struct spu_context *ctx)
  157. {
  158. BUG_ON(ctx->state != SPU_STATE_SAVED);
  159. if (test_and_clear_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags) &&
  160. test_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags))
  161. spu_activate(ctx, 0);
  162. spu_release(ctx);
  163. }