167207b96SArnd Bergmann /*
267207b96SArnd Bergmann  * SPU file system -- SPU context management
367207b96SArnd Bergmann  *
467207b96SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
567207b96SArnd Bergmann  *
667207b96SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
767207b96SArnd Bergmann  *
867207b96SArnd Bergmann  * This program is free software; you can redistribute it and/or modify
967207b96SArnd Bergmann  * it under the terms of the GNU General Public License as published by
1067207b96SArnd Bergmann  * the Free Software Foundation; either version 2, or (at your option)
1167207b96SArnd Bergmann  * any later version.
1267207b96SArnd Bergmann  *
1367207b96SArnd Bergmann  * This program is distributed in the hope that it will be useful,
1467207b96SArnd Bergmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1567207b96SArnd Bergmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1667207b96SArnd Bergmann  * GNU General Public License for more details.
1767207b96SArnd Bergmann  *
1867207b96SArnd Bergmann  * You should have received a copy of the GNU General Public License
1967207b96SArnd Bergmann  * along with this program; if not, write to the Free Software
2067207b96SArnd Bergmann  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2167207b96SArnd Bergmann  */
2267207b96SArnd Bergmann 
238b3d6663SArnd Bergmann #include <linux/fs.h>
248b3d6663SArnd Bergmann #include <linux/mm.h>
2567207b96SArnd Bergmann #include <linux/slab.h>
2667207b96SArnd Bergmann #include <asm/spu.h>
275473af04SMark Nutter #include <asm/spu_csa.h>
2867207b96SArnd Bergmann #include "spufs.h"
2967207b96SArnd Bergmann 
308b3d6663SArnd Bergmann struct spu_context *alloc_spu_context(struct address_space *local_store)
3167207b96SArnd Bergmann {
3267207b96SArnd Bergmann 	struct spu_context *ctx;
3367207b96SArnd Bergmann 	ctx = kmalloc(sizeof *ctx, GFP_KERNEL);
3467207b96SArnd Bergmann 	if (!ctx)
3567207b96SArnd Bergmann 		goto out;
368b3d6663SArnd Bergmann 	/* Binding to physical processor deferred
378b3d6663SArnd Bergmann 	 * until spu_activate().
385473af04SMark Nutter 	 */
395473af04SMark Nutter 	spu_init_csa(&ctx->csa);
405473af04SMark Nutter 	if (!ctx->csa.lscsa) {
415473af04SMark Nutter 		goto out_free;
425473af04SMark Nutter 	}
4367207b96SArnd Bergmann 	spin_lock_init(&ctx->mmio_lock);
4467207b96SArnd Bergmann 	kref_init(&ctx->kref);
458b3d6663SArnd Bergmann 	init_rwsem(&ctx->state_sema);
468b3d6663SArnd Bergmann 	init_waitqueue_head(&ctx->ibox_wq);
478b3d6663SArnd Bergmann 	init_waitqueue_head(&ctx->wbox_wq);
488b3d6663SArnd Bergmann 	ctx->ibox_fasync = NULL;
498b3d6663SArnd Bergmann 	ctx->wbox_fasync = NULL;
508b3d6663SArnd Bergmann 	ctx->state = SPU_STATE_SAVED;
518b3d6663SArnd Bergmann 	ctx->local_store = local_store;
528b3d6663SArnd Bergmann 	ctx->spu = NULL;
538b3d6663SArnd Bergmann 	ctx->ops = &spu_backing_ops;
548b3d6663SArnd Bergmann 	ctx->owner = get_task_mm(current);
5567207b96SArnd Bergmann 	goto out;
5667207b96SArnd Bergmann out_free:
5767207b96SArnd Bergmann 	kfree(ctx);
5867207b96SArnd Bergmann 	ctx = NULL;
5967207b96SArnd Bergmann out:
6067207b96SArnd Bergmann 	return ctx;
6167207b96SArnd Bergmann }
6267207b96SArnd Bergmann 
6367207b96SArnd Bergmann void destroy_spu_context(struct kref *kref)
6467207b96SArnd Bergmann {
6567207b96SArnd Bergmann 	struct spu_context *ctx;
6667207b96SArnd Bergmann 	ctx = container_of(kref, struct spu_context, kref);
678b3d6663SArnd Bergmann 	down_write(&ctx->state_sema);
688b3d6663SArnd Bergmann 	spu_deactivate(ctx);
698b3d6663SArnd Bergmann 	ctx->ibox_fasync = NULL;
708b3d6663SArnd Bergmann 	ctx->wbox_fasync = NULL;
718b3d6663SArnd Bergmann 	up_write(&ctx->state_sema);
725473af04SMark Nutter 	spu_fini_csa(&ctx->csa);
7367207b96SArnd Bergmann 	kfree(ctx);
7467207b96SArnd Bergmann }
7567207b96SArnd Bergmann 
7667207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx)
7767207b96SArnd Bergmann {
7867207b96SArnd Bergmann 	kref_get(&ctx->kref);
7967207b96SArnd Bergmann 	return ctx;
8067207b96SArnd Bergmann }
8167207b96SArnd Bergmann 
8267207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx)
8367207b96SArnd Bergmann {
8467207b96SArnd Bergmann 	return kref_put(&ctx->kref, &destroy_spu_context);
8567207b96SArnd Bergmann }
8667207b96SArnd Bergmann 
878b3d6663SArnd Bergmann /* give up the mm reference when the context is about to be destroyed */
888b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx)
898b3d6663SArnd Bergmann {
908b3d6663SArnd Bergmann 	struct mm_struct *mm;
918b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
928b3d6663SArnd Bergmann 	mm = ctx->owner;
938b3d6663SArnd Bergmann 	ctx->owner = NULL;
948b3d6663SArnd Bergmann 	mmput(mm);
958b3d6663SArnd Bergmann 	spu_release(ctx);
968b3d6663SArnd Bergmann }
9767207b96SArnd Bergmann 
988b3d6663SArnd Bergmann void spu_acquire(struct spu_context *ctx)
998b3d6663SArnd Bergmann {
1008b3d6663SArnd Bergmann 	down_read(&ctx->state_sema);
1018b3d6663SArnd Bergmann }
1028b3d6663SArnd Bergmann 
1038b3d6663SArnd Bergmann void spu_release(struct spu_context *ctx)
1048b3d6663SArnd Bergmann {
1058b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1068b3d6663SArnd Bergmann }
1078b3d6663SArnd Bergmann 
1088b3d6663SArnd Bergmann static void spu_unmap_mappings(struct spu_context *ctx)
1098b3d6663SArnd Bergmann {
1108b3d6663SArnd Bergmann 	unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1);
1118b3d6663SArnd Bergmann }
1128b3d6663SArnd Bergmann 
1138b3d6663SArnd Bergmann int spu_acquire_runnable(struct spu_context *ctx)
1148b3d6663SArnd Bergmann {
1158b3d6663SArnd Bergmann 	int ret = 0;
1168b3d6663SArnd Bergmann 
1178b3d6663SArnd Bergmann 	down_read(&ctx->state_sema);
1188b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_RUNNABLE)
1198b3d6663SArnd Bergmann 		return 0;
1208b3d6663SArnd Bergmann 	/* ctx is about to be freed, can't acquire any more */
1218b3d6663SArnd Bergmann 	if (!ctx->owner) {
1228b3d6663SArnd Bergmann 		ret = -EINVAL;
1238b3d6663SArnd Bergmann 		goto out;
1248b3d6663SArnd Bergmann 	}
1258b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1268b3d6663SArnd Bergmann 
1278b3d6663SArnd Bergmann 	down_write(&ctx->state_sema);
1288b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED) {
1298b3d6663SArnd Bergmann 		spu_unmap_mappings(ctx);
1308b3d6663SArnd Bergmann 		ret = spu_activate(ctx, 0);
1318b3d6663SArnd Bergmann 		ctx->state = SPU_STATE_RUNNABLE;
1328b3d6663SArnd Bergmann 	}
1338b3d6663SArnd Bergmann 	downgrade_write(&ctx->state_sema);
1348b3d6663SArnd Bergmann 	if (ret)
1358b3d6663SArnd Bergmann 		goto out;
1368b3d6663SArnd Bergmann 
1378b3d6663SArnd Bergmann 	/* On success, we return holding the lock */
1388b3d6663SArnd Bergmann 	return ret;
1398b3d6663SArnd Bergmann out:
1408b3d6663SArnd Bergmann 	/* Release here, to simplify calling code. */
1418b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1428b3d6663SArnd Bergmann 
1438b3d6663SArnd Bergmann 	return ret;
1448b3d6663SArnd Bergmann }
1458b3d6663SArnd Bergmann 
1468b3d6663SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx)
1478b3d6663SArnd Bergmann {
1488b3d6663SArnd Bergmann 	down_read(&ctx->state_sema);
1498b3d6663SArnd Bergmann 
1508b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED)
1518b3d6663SArnd Bergmann 		return;
1528b3d6663SArnd Bergmann 
1538b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1548b3d6663SArnd Bergmann 	down_write(&ctx->state_sema);
1558b3d6663SArnd Bergmann 
1568b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_RUNNABLE) {
1578b3d6663SArnd Bergmann 		spu_unmap_mappings(ctx);
1588b3d6663SArnd Bergmann 		spu_deactivate(ctx);
1598b3d6663SArnd Bergmann 		ctx->state = SPU_STATE_SAVED;
1608b3d6663SArnd Bergmann 	}
1618b3d6663SArnd Bergmann 
1628b3d6663SArnd Bergmann 	downgrade_write(&ctx->state_sema);
1638b3d6663SArnd Bergmann }
164