1de6cc651SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
267207b96SArnd Bergmann /*
367207b96SArnd Bergmann  * SPU file system -- SPU context management
467207b96SArnd Bergmann  *
567207b96SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
667207b96SArnd Bergmann  *
767207b96SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
867207b96SArnd Bergmann  */
967207b96SArnd Bergmann 
108b3d6663SArnd Bergmann #include <linux/fs.h>
118b3d6663SArnd Bergmann #include <linux/mm.h>
1267207b96SArnd Bergmann #include <linux/slab.h>
1360063497SArun Sharma #include <linux/atomic.h>
1462fe91bbSPaul Gortmaker #include <linux/sched.h>
156e84f315SIngo Molnar #include <linux/sched/mm.h>
166e84f315SIngo Molnar 
1767207b96SArnd Bergmann #include <asm/spu.h>
185473af04SMark Nutter #include <asm/spu_csa.h>
1967207b96SArnd Bergmann #include "spufs.h"
20ae142e0cSChristoph Hellwig #include "sputrace.h"
2167207b96SArnd Bergmann 
2265de66f0SChristoph Hellwig 
2365de66f0SChristoph Hellwig atomic_t nr_spu_contexts = ATOMIC_INIT(0);
2465de66f0SChristoph Hellwig 
alloc_spu_context(struct spu_gang * gang)256263203eSArnd Bergmann struct spu_context *alloc_spu_context(struct spu_gang *gang)
2667207b96SArnd Bergmann {
2767207b96SArnd Bergmann 	struct spu_context *ctx;
288f748aaeSArnd Bergmann 
29c5c45913SJeremy Kerr 	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
3067207b96SArnd Bergmann 	if (!ctx)
3167207b96SArnd Bergmann 		goto out;
328b3d6663SArnd Bergmann 	/* Binding to physical processor deferred
338b3d6663SArnd Bergmann 	 * until spu_activate().
345473af04SMark Nutter 	 */
35f1fa74f4SBenjamin Herrenschmidt 	if (spu_init_csa(&ctx->csa))
365473af04SMark Nutter 		goto out_free;
3767207b96SArnd Bergmann 	spin_lock_init(&ctx->mmio_lock);
3847d3a5faSChristoph Hellwig 	mutex_init(&ctx->mapping_lock);
3967207b96SArnd Bergmann 	kref_init(&ctx->kref);
40650f8b02SChristoph Hellwig 	mutex_init(&ctx->state_mutex);
41e45d48a3SChristoph Hellwig 	mutex_init(&ctx->run_mutex);
428b3d6663SArnd Bergmann 	init_waitqueue_head(&ctx->ibox_wq);
438b3d6663SArnd Bergmann 	init_waitqueue_head(&ctx->wbox_wq);
445110459fSArnd Bergmann 	init_waitqueue_head(&ctx->stop_wq);
45a33a7d73SArnd Bergmann 	init_waitqueue_head(&ctx->mfc_wq);
4633bfd7a7SArnd Bergmann 	init_waitqueue_head(&ctx->run_wq);
478b3d6663SArnd Bergmann 	ctx->state = SPU_STATE_SAVED;
488b3d6663SArnd Bergmann 	ctx->ops = &spu_backing_ops;
498b3d6663SArnd Bergmann 	ctx->owner = get_task_mm(current);
50a475c2f4SChristoph Hellwig 	INIT_LIST_HEAD(&ctx->rq);
518e68e2f2SArnd Bergmann 	INIT_LIST_HEAD(&ctx->aff_list);
526263203eSArnd Bergmann 	if (gang)
536263203eSArnd Bergmann 		spu_gang_add_ctx(gang, ctx);
549d78592eSChristoph Hellwig 
559d78592eSChristoph Hellwig 	__spu_update_sched_info(ctx);
56fe443ef2SChristoph Hellwig 	spu_set_timeslice(ctx);
5727ec41d3SAndre Detsch 	ctx->stats.util_state = SPU_UTIL_IDLE_LOADED;
58f2dec1eaSThomas Gleixner 	ctx->stats.tstamp = ktime_get_ns();
5965de66f0SChristoph Hellwig 
6065de66f0SChristoph Hellwig 	atomic_inc(&nr_spu_contexts);
6167207b96SArnd Bergmann 	goto out;
6267207b96SArnd Bergmann out_free:
6367207b96SArnd Bergmann 	kfree(ctx);
6467207b96SArnd Bergmann 	ctx = NULL;
6567207b96SArnd Bergmann out:
6667207b96SArnd Bergmann 	return ctx;
6767207b96SArnd Bergmann }
6867207b96SArnd Bergmann 
destroy_spu_context(struct kref * kref)6967207b96SArnd Bergmann void destroy_spu_context(struct kref *kref)
7067207b96SArnd Bergmann {
7167207b96SArnd Bergmann 	struct spu_context *ctx;
7267207b96SArnd Bergmann 	ctx = container_of(kref, struct spu_context, kref);
7353457881SJulio M. Merino Vidal 	spu_context_nospu_trace(destroy_spu_context__enter, ctx);
74650f8b02SChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
758b3d6663SArnd Bergmann 	spu_deactivate(ctx);
76650f8b02SChristoph Hellwig 	mutex_unlock(&ctx->state_mutex);
775473af04SMark Nutter 	spu_fini_csa(&ctx->csa);
786263203eSArnd Bergmann 	if (ctx->gang)
796263203eSArnd Bergmann 		spu_gang_remove_ctx(ctx->gang, ctx);
801474855dSBob Nelson 	if (ctx->prof_priv_kref)
811474855dSBob Nelson 		kref_put(ctx->prof_priv_kref, ctx->prof_priv_release);
82a475c2f4SChristoph Hellwig 	BUG_ON(!list_empty(&ctx->rq));
8365de66f0SChristoph Hellwig 	atomic_dec(&nr_spu_contexts);
845158e9b5SChristoph Hellwig 	kfree(ctx->switch_log);
8567207b96SArnd Bergmann 	kfree(ctx);
8667207b96SArnd Bergmann }
8767207b96SArnd Bergmann 
get_spu_context(struct spu_context * ctx)8867207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx)
8967207b96SArnd Bergmann {
9067207b96SArnd Bergmann 	kref_get(&ctx->kref);
9167207b96SArnd Bergmann 	return ctx;
9267207b96SArnd Bergmann }
9367207b96SArnd Bergmann 
put_spu_context(struct spu_context * ctx)9467207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx)
9567207b96SArnd Bergmann {
9667207b96SArnd Bergmann 	return kref_put(&ctx->kref, &destroy_spu_context);
9767207b96SArnd Bergmann }
9867207b96SArnd Bergmann 
998b3d6663SArnd Bergmann /* give up the mm reference when the context is about to be destroyed */
spu_forget(struct spu_context * ctx)1008b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx)
1018b3d6663SArnd Bergmann {
1028b3d6663SArnd Bergmann 	struct mm_struct *mm;
103c9101bdbSChristoph Hellwig 
104c9101bdbSChristoph Hellwig 	/*
105c9101bdbSChristoph Hellwig 	 * This is basically an open-coded spu_acquire_saved, except that
1060111a701SJeremy Kerr 	 * we don't acquire the state mutex interruptible, and we don't
1070111a701SJeremy Kerr 	 * want this context to be rescheduled on release.
108c9101bdbSChristoph Hellwig 	 */
109c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
1100111a701SJeremy Kerr 	if (ctx->state != SPU_STATE_SAVED)
111c9101bdbSChristoph Hellwig 		spu_deactivate(ctx);
112c9101bdbSChristoph Hellwig 
1138b3d6663SArnd Bergmann 	mm = ctx->owner;
1148b3d6663SArnd Bergmann 	ctx->owner = NULL;
1158b3d6663SArnd Bergmann 	mmput(mm);
1168b3d6663SArnd Bergmann 	spu_release(ctx);
1178b3d6663SArnd Bergmann }
11867207b96SArnd Bergmann 
spu_unmap_mappings(struct spu_context * ctx)1195110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx)
1208b3d6663SArnd Bergmann {
12147d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
1226df10a82SMark Nutter 	if (ctx->local_store)
1238b3d6663SArnd Bergmann 		unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1);
1246df10a82SMark Nutter 	if (ctx->mfc)
12587ff6090SJeremy Kerr 		unmap_mapping_range(ctx->mfc, 0, SPUFS_MFC_MAP_SIZE, 1);
1266df10a82SMark Nutter 	if (ctx->cntl)
12787ff6090SJeremy Kerr 		unmap_mapping_range(ctx->cntl, 0, SPUFS_CNTL_MAP_SIZE, 1);
1286df10a82SMark Nutter 	if (ctx->signal1)
12987ff6090SJeremy Kerr 		unmap_mapping_range(ctx->signal1, 0, SPUFS_SIGNAL_MAP_SIZE, 1);
1306df10a82SMark Nutter 	if (ctx->signal2)
13187ff6090SJeremy Kerr 		unmap_mapping_range(ctx->signal2, 0, SPUFS_SIGNAL_MAP_SIZE, 1);
13217e0e270SBenjamin Herrenschmidt 	if (ctx->mss)
13387ff6090SJeremy Kerr 		unmap_mapping_range(ctx->mss, 0, SPUFS_MSS_MAP_SIZE, 1);
13417e0e270SBenjamin Herrenschmidt 	if (ctx->psmap)
13587ff6090SJeremy Kerr 		unmap_mapping_range(ctx->psmap, 0, SPUFS_PS_MAP_SIZE, 1);
13647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1378b3d6663SArnd Bergmann }
1388b3d6663SArnd Bergmann 
1396a0641e5SChristoph Hellwig /**
1406a0641e5SChristoph Hellwig  * spu_acquire_saved - lock spu contex and make sure it is in saved state
1416a0641e5SChristoph Hellwig  * @ctx:	spu contex to lock
1426a0641e5SChristoph Hellwig  */
spu_acquire_saved(struct spu_context * ctx)143c9101bdbSChristoph Hellwig int spu_acquire_saved(struct spu_context *ctx)
1448b3d6663SArnd Bergmann {
145c9101bdbSChristoph Hellwig 	int ret;
146c9101bdbSChristoph Hellwig 
1473734dfc6SJulio M. Merino Vidal 	spu_context_nospu_trace(spu_acquire_saved__enter, ctx);
1483734dfc6SJulio M. Merino Vidal 
149c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
150c9101bdbSChristoph Hellwig 	if (ret)
151c9101bdbSChristoph Hellwig 		return ret;
152c9101bdbSChristoph Hellwig 
15327b1ea09SChristoph Hellwig 	if (ctx->state != SPU_STATE_SAVED) {
15427b1ea09SChristoph Hellwig 		set_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags);
1558b3d6663SArnd Bergmann 		spu_deactivate(ctx);
1568b3d6663SArnd Bergmann 	}
157c9101bdbSChristoph Hellwig 
158c9101bdbSChristoph Hellwig 	return 0;
15927b1ea09SChristoph Hellwig }
16027b1ea09SChristoph Hellwig 
16127b1ea09SChristoph Hellwig /**
16227b1ea09SChristoph Hellwig  * spu_release_saved - unlock spu context and return it to the runqueue
16327b1ea09SChristoph Hellwig  * @ctx:	context to unlock
16427b1ea09SChristoph Hellwig  */
spu_release_saved(struct spu_context * ctx)16527b1ea09SChristoph Hellwig void spu_release_saved(struct spu_context *ctx)
16627b1ea09SChristoph Hellwig {
16727b1ea09SChristoph Hellwig 	BUG_ON(ctx->state != SPU_STATE_SAVED);
16827b1ea09SChristoph Hellwig 
169c368392aSJeremy Kerr 	if (test_and_clear_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags) &&
170c368392aSJeremy Kerr 			test_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags))
17127b1ea09SChristoph Hellwig 		spu_activate(ctx, 0);
17227b1ea09SChristoph Hellwig 
17327b1ea09SChristoph Hellwig 	spu_release(ctx);
17427b1ea09SChristoph Hellwig }
1751474855dSBob Nelson 
176