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); 465ef8224aSArnd Bergmann init_MUTEX(&ctx->run_sema); 478b3d6663SArnd Bergmann init_waitqueue_head(&ctx->ibox_wq); 488b3d6663SArnd Bergmann init_waitqueue_head(&ctx->wbox_wq); 495110459fSArnd Bergmann init_waitqueue_head(&ctx->stop_wq); 50a33a7d73SArnd Bergmann init_waitqueue_head(&ctx->mfc_wq); 518b3d6663SArnd Bergmann ctx->ibox_fasync = NULL; 528b3d6663SArnd Bergmann ctx->wbox_fasync = NULL; 53a33a7d73SArnd Bergmann ctx->mfc_fasync = NULL; 54a33a7d73SArnd Bergmann ctx->tagwait = 0; 558b3d6663SArnd Bergmann ctx->state = SPU_STATE_SAVED; 568b3d6663SArnd Bergmann ctx->local_store = local_store; 578b3d6663SArnd Bergmann ctx->spu = NULL; 588b3d6663SArnd Bergmann ctx->ops = &spu_backing_ops; 598b3d6663SArnd Bergmann ctx->owner = get_task_mm(current); 6067207b96SArnd Bergmann goto out; 6167207b96SArnd Bergmann out_free: 6267207b96SArnd Bergmann kfree(ctx); 6367207b96SArnd Bergmann ctx = NULL; 6467207b96SArnd Bergmann out: 6567207b96SArnd Bergmann return ctx; 6667207b96SArnd Bergmann } 6767207b96SArnd Bergmann 6867207b96SArnd Bergmann void destroy_spu_context(struct kref *kref) 6967207b96SArnd Bergmann { 7067207b96SArnd Bergmann struct spu_context *ctx; 7167207b96SArnd Bergmann ctx = container_of(kref, struct spu_context, kref); 728b3d6663SArnd Bergmann down_write(&ctx->state_sema); 738b3d6663SArnd Bergmann spu_deactivate(ctx); 748b3d6663SArnd Bergmann up_write(&ctx->state_sema); 755473af04SMark Nutter spu_fini_csa(&ctx->csa); 7667207b96SArnd Bergmann kfree(ctx); 7767207b96SArnd Bergmann } 7867207b96SArnd Bergmann 7967207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx) 8067207b96SArnd Bergmann { 8167207b96SArnd Bergmann kref_get(&ctx->kref); 8267207b96SArnd Bergmann return ctx; 8367207b96SArnd Bergmann } 8467207b96SArnd Bergmann 8567207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx) 8667207b96SArnd Bergmann { 8767207b96SArnd Bergmann return kref_put(&ctx->kref, &destroy_spu_context); 8867207b96SArnd Bergmann } 8967207b96SArnd Bergmann 908b3d6663SArnd Bergmann /* give up the mm reference when the context is about to be destroyed */ 918b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx) 928b3d6663SArnd Bergmann { 938b3d6663SArnd Bergmann struct mm_struct *mm; 948b3d6663SArnd Bergmann spu_acquire_saved(ctx); 958b3d6663SArnd Bergmann mm = ctx->owner; 968b3d6663SArnd Bergmann ctx->owner = NULL; 978b3d6663SArnd Bergmann mmput(mm); 988b3d6663SArnd Bergmann spu_release(ctx); 998b3d6663SArnd Bergmann } 10067207b96SArnd Bergmann 1018b3d6663SArnd Bergmann void spu_acquire(struct spu_context *ctx) 1028b3d6663SArnd Bergmann { 1038b3d6663SArnd Bergmann down_read(&ctx->state_sema); 1048b3d6663SArnd Bergmann } 1058b3d6663SArnd Bergmann 1068b3d6663SArnd Bergmann void spu_release(struct spu_context *ctx) 1078b3d6663SArnd Bergmann { 1088b3d6663SArnd Bergmann up_read(&ctx->state_sema); 1098b3d6663SArnd Bergmann } 1108b3d6663SArnd Bergmann 1115110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx) 1128b3d6663SArnd Bergmann { 1138b3d6663SArnd Bergmann unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1); 1148b3d6663SArnd Bergmann } 1158b3d6663SArnd Bergmann 1168b3d6663SArnd Bergmann int spu_acquire_runnable(struct spu_context *ctx) 1178b3d6663SArnd Bergmann { 1188b3d6663SArnd Bergmann int ret = 0; 1198b3d6663SArnd Bergmann 1208b3d6663SArnd Bergmann down_read(&ctx->state_sema); 1212a911f0bSArnd Bergmann if (ctx->state == SPU_STATE_RUNNABLE) { 1222a911f0bSArnd Bergmann ctx->spu->prio = current->prio; 1238b3d6663SArnd Bergmann return 0; 1242a911f0bSArnd Bergmann } 125762cf6daSArnd Bergmann up_read(&ctx->state_sema); 126762cf6daSArnd Bergmann 127762cf6daSArnd Bergmann down_write(&ctx->state_sema); 1288b3d6663SArnd Bergmann /* ctx is about to be freed, can't acquire any more */ 1298b3d6663SArnd Bergmann if (!ctx->owner) { 1308b3d6663SArnd Bergmann ret = -EINVAL; 1318b3d6663SArnd Bergmann goto out; 1328b3d6663SArnd Bergmann } 1338b3d6663SArnd Bergmann 1348b3d6663SArnd Bergmann if (ctx->state == SPU_STATE_SAVED) { 1358b3d6663SArnd Bergmann ret = spu_activate(ctx, 0); 1368b3d6663SArnd Bergmann if (ret) 1378b3d6663SArnd Bergmann goto out; 13801062465SArnd Bergmann ctx->state = SPU_STATE_RUNNABLE; 13901062465SArnd Bergmann } 1408b3d6663SArnd Bergmann 141762cf6daSArnd Bergmann downgrade_write(&ctx->state_sema); 1428b3d6663SArnd Bergmann /* On success, we return holding the lock */ 143762cf6daSArnd Bergmann 1448b3d6663SArnd Bergmann return ret; 1458b3d6663SArnd Bergmann out: 1468b3d6663SArnd Bergmann /* Release here, to simplify calling code. */ 147762cf6daSArnd Bergmann up_write(&ctx->state_sema); 1488b3d6663SArnd Bergmann 1498b3d6663SArnd Bergmann return ret; 1508b3d6663SArnd Bergmann } 1518b3d6663SArnd Bergmann 1528b3d6663SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx) 1538b3d6663SArnd Bergmann { 1548b3d6663SArnd Bergmann down_read(&ctx->state_sema); 1558b3d6663SArnd Bergmann 1568b3d6663SArnd Bergmann if (ctx->state == SPU_STATE_SAVED) 1578b3d6663SArnd Bergmann return; 1588b3d6663SArnd Bergmann 1598b3d6663SArnd Bergmann up_read(&ctx->state_sema); 1608b3d6663SArnd Bergmann down_write(&ctx->state_sema); 1618b3d6663SArnd Bergmann 1628b3d6663SArnd Bergmann if (ctx->state == SPU_STATE_RUNNABLE) { 1638b3d6663SArnd Bergmann spu_deactivate(ctx); 1648b3d6663SArnd Bergmann ctx->state = SPU_STATE_SAVED; 1658b3d6663SArnd Bergmann } 1668b3d6663SArnd Bergmann 1678b3d6663SArnd Bergmann downgrade_write(&ctx->state_sema); 1688b3d6663SArnd Bergmann } 169