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);
485110459fSArnd Bergmann 	init_waitqueue_head(&ctx->stop_wq);
498b3d6663SArnd Bergmann 	ctx->ibox_fasync = NULL;
508b3d6663SArnd Bergmann 	ctx->wbox_fasync = NULL;
518b3d6663SArnd Bergmann 	ctx->state = SPU_STATE_SAVED;
528b3d6663SArnd Bergmann 	ctx->local_store = local_store;
538b3d6663SArnd Bergmann 	ctx->spu = NULL;
548b3d6663SArnd Bergmann 	ctx->ops = &spu_backing_ops;
558b3d6663SArnd Bergmann 	ctx->owner = get_task_mm(current);
5667207b96SArnd Bergmann 	goto out;
5767207b96SArnd Bergmann out_free:
5867207b96SArnd Bergmann 	kfree(ctx);
5967207b96SArnd Bergmann 	ctx = NULL;
6067207b96SArnd Bergmann out:
6167207b96SArnd Bergmann 	return ctx;
6267207b96SArnd Bergmann }
6367207b96SArnd Bergmann 
6467207b96SArnd Bergmann void destroy_spu_context(struct kref *kref)
6567207b96SArnd Bergmann {
6667207b96SArnd Bergmann 	struct spu_context *ctx;
6767207b96SArnd Bergmann 	ctx = container_of(kref, struct spu_context, kref);
688b3d6663SArnd Bergmann 	down_write(&ctx->state_sema);
698b3d6663SArnd Bergmann 	spu_deactivate(ctx);
708b3d6663SArnd Bergmann 	ctx->ibox_fasync = NULL;
718b3d6663SArnd Bergmann 	ctx->wbox_fasync = NULL;
728b3d6663SArnd Bergmann 	up_write(&ctx->state_sema);
735473af04SMark Nutter 	spu_fini_csa(&ctx->csa);
7467207b96SArnd Bergmann 	kfree(ctx);
7567207b96SArnd Bergmann }
7667207b96SArnd Bergmann 
7767207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx)
7867207b96SArnd Bergmann {
7967207b96SArnd Bergmann 	kref_get(&ctx->kref);
8067207b96SArnd Bergmann 	return ctx;
8167207b96SArnd Bergmann }
8267207b96SArnd Bergmann 
8367207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx)
8467207b96SArnd Bergmann {
8567207b96SArnd Bergmann 	return kref_put(&ctx->kref, &destroy_spu_context);
8667207b96SArnd Bergmann }
8767207b96SArnd Bergmann 
888b3d6663SArnd Bergmann /* give up the mm reference when the context is about to be destroyed */
898b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx)
908b3d6663SArnd Bergmann {
918b3d6663SArnd Bergmann 	struct mm_struct *mm;
928b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
938b3d6663SArnd Bergmann 	mm = ctx->owner;
948b3d6663SArnd Bergmann 	ctx->owner = NULL;
958b3d6663SArnd Bergmann 	mmput(mm);
968b3d6663SArnd Bergmann 	spu_release(ctx);
978b3d6663SArnd Bergmann }
9867207b96SArnd Bergmann 
998b3d6663SArnd Bergmann void spu_acquire(struct spu_context *ctx)
1008b3d6663SArnd Bergmann {
1018b3d6663SArnd Bergmann 	down_read(&ctx->state_sema);
1028b3d6663SArnd Bergmann }
1038b3d6663SArnd Bergmann 
1048b3d6663SArnd Bergmann void spu_release(struct spu_context *ctx)
1058b3d6663SArnd Bergmann {
1068b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1078b3d6663SArnd Bergmann }
1088b3d6663SArnd Bergmann 
1095110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx)
1108b3d6663SArnd Bergmann {
1118b3d6663SArnd Bergmann 	unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1);
1128b3d6663SArnd Bergmann }
1138b3d6663SArnd Bergmann 
1148b3d6663SArnd Bergmann int spu_acquire_runnable(struct spu_context *ctx)
1158b3d6663SArnd Bergmann {
1168b3d6663SArnd Bergmann 	int ret = 0;
1178b3d6663SArnd Bergmann 
1188b3d6663SArnd Bergmann 	down_read(&ctx->state_sema);
1192a911f0bSArnd Bergmann 	if (ctx->state == SPU_STATE_RUNNABLE) {
1202a911f0bSArnd Bergmann 		ctx->spu->prio = current->prio;
1218b3d6663SArnd Bergmann 		return 0;
1222a911f0bSArnd Bergmann 	}
1238b3d6663SArnd Bergmann 	/* ctx is about to be freed, can't acquire any more */
1248b3d6663SArnd Bergmann 	if (!ctx->owner) {
1258b3d6663SArnd Bergmann 		ret = -EINVAL;
1268b3d6663SArnd Bergmann 		goto out;
1278b3d6663SArnd Bergmann 	}
1288b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1298b3d6663SArnd Bergmann 
1308b3d6663SArnd Bergmann 	down_write(&ctx->state_sema);
1318b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED) {
1328b3d6663SArnd Bergmann 		ret = spu_activate(ctx, 0);
1338b3d6663SArnd Bergmann 		ctx->state = SPU_STATE_RUNNABLE;
1348b3d6663SArnd Bergmann 	}
1358b3d6663SArnd Bergmann 	downgrade_write(&ctx->state_sema);
1368b3d6663SArnd Bergmann 	if (ret)
1378b3d6663SArnd Bergmann 		goto out;
1388b3d6663SArnd Bergmann 
1398b3d6663SArnd Bergmann 	/* On success, we return holding the lock */
1408b3d6663SArnd Bergmann 	return ret;
1418b3d6663SArnd Bergmann out:
1428b3d6663SArnd Bergmann 	/* Release here, to simplify calling code. */
1438b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1448b3d6663SArnd Bergmann 
1458b3d6663SArnd Bergmann 	return ret;
1468b3d6663SArnd Bergmann }
1478b3d6663SArnd Bergmann 
1488b3d6663SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx)
1498b3d6663SArnd Bergmann {
1508b3d6663SArnd Bergmann 	down_read(&ctx->state_sema);
1518b3d6663SArnd Bergmann 
1528b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED)
1538b3d6663SArnd Bergmann 		return;
1548b3d6663SArnd Bergmann 
1558b3d6663SArnd Bergmann 	up_read(&ctx->state_sema);
1568b3d6663SArnd Bergmann 	down_write(&ctx->state_sema);
1578b3d6663SArnd Bergmann 
1588b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_RUNNABLE) {
1598b3d6663SArnd Bergmann 		spu_deactivate(ctx);
1608b3d6663SArnd Bergmann 		ctx->state = SPU_STATE_SAVED;
1618b3d6663SArnd Bergmann 	}
1628b3d6663SArnd Bergmann 
1638b3d6663SArnd Bergmann 	downgrade_write(&ctx->state_sema);
1648b3d6663SArnd Bergmann }
165