/arch/powerpc/platforms/cell/spufs/context.c

http://github.com/mirrors/linux · C · 175 lines · 126 code · 24 blank · 25 comment · 19 complexity · 813a2a5f0c51f7972093d88eb58a8147 MD5 · raw file

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