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 306df10a82SMark Nutter struct spu_context *alloc_spu_context(void) 3167207b96SArnd Bergmann { 3267207b96SArnd Bergmann struct spu_context *ctx; 33c5c45913SJeremy Kerr ctx = kzalloc(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->state = SPU_STATE_SAVED; 528b3d6663SArnd Bergmann ctx->ops = &spu_backing_ops; 538b3d6663SArnd Bergmann ctx->owner = get_task_mm(current); 5467207b96SArnd Bergmann goto out; 5567207b96SArnd Bergmann out_free: 5667207b96SArnd Bergmann kfree(ctx); 5767207b96SArnd Bergmann ctx = NULL; 5867207b96SArnd Bergmann out: 5967207b96SArnd Bergmann return ctx; 6067207b96SArnd Bergmann } 6167207b96SArnd Bergmann 6267207b96SArnd Bergmann void destroy_spu_context(struct kref *kref) 6367207b96SArnd Bergmann { 6467207b96SArnd Bergmann struct spu_context *ctx; 6567207b96SArnd Bergmann ctx = container_of(kref, struct spu_context, kref); 668b3d6663SArnd Bergmann down_write(&ctx->state_sema); 678b3d6663SArnd Bergmann spu_deactivate(ctx); 688b3d6663SArnd Bergmann up_write(&ctx->state_sema); 695473af04SMark Nutter spu_fini_csa(&ctx->csa); 7067207b96SArnd Bergmann kfree(ctx); 7167207b96SArnd Bergmann } 7267207b96SArnd Bergmann 7367207b96SArnd Bergmann struct spu_context * get_spu_context(struct spu_context *ctx) 7467207b96SArnd Bergmann { 7567207b96SArnd Bergmann kref_get(&ctx->kref); 7667207b96SArnd Bergmann return ctx; 7767207b96SArnd Bergmann } 7867207b96SArnd Bergmann 7967207b96SArnd Bergmann int put_spu_context(struct spu_context *ctx) 8067207b96SArnd Bergmann { 8167207b96SArnd Bergmann return kref_put(&ctx->kref, &destroy_spu_context); 8267207b96SArnd Bergmann } 8367207b96SArnd Bergmann 848b3d6663SArnd Bergmann /* give up the mm reference when the context is about to be destroyed */ 858b3d6663SArnd Bergmann void spu_forget(struct spu_context *ctx) 868b3d6663SArnd Bergmann { 878b3d6663SArnd Bergmann struct mm_struct *mm; 888b3d6663SArnd Bergmann spu_acquire_saved(ctx); 898b3d6663SArnd Bergmann mm = ctx->owner; 908b3d6663SArnd Bergmann ctx->owner = NULL; 918b3d6663SArnd Bergmann mmput(mm); 928b3d6663SArnd Bergmann spu_release(ctx); 938b3d6663SArnd Bergmann } 9467207b96SArnd Bergmann 958b3d6663SArnd Bergmann void spu_acquire(struct spu_context *ctx) 968b3d6663SArnd Bergmann { 978b3d6663SArnd Bergmann down_read(&ctx->state_sema); 988b3d6663SArnd Bergmann } 998b3d6663SArnd Bergmann 1008b3d6663SArnd Bergmann void spu_release(struct spu_context *ctx) 1018b3d6663SArnd Bergmann { 1028b3d6663SArnd Bergmann up_read(&ctx->state_sema); 1038b3d6663SArnd Bergmann } 1048b3d6663SArnd Bergmann 1055110459fSArnd Bergmann void spu_unmap_mappings(struct spu_context *ctx) 1068b3d6663SArnd Bergmann { 1076df10a82SMark Nutter if (ctx->local_store) 1088b3d6663SArnd Bergmann unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1); 1096df10a82SMark Nutter if (ctx->mfc) 1106df10a82SMark Nutter unmap_mapping_range(ctx->mfc, 0, 0x4000, 1); 1116df10a82SMark Nutter if (ctx->cntl) 1126df10a82SMark Nutter unmap_mapping_range(ctx->cntl, 0, 0x4000, 1); 1136df10a82SMark Nutter if (ctx->signal1) 1146df10a82SMark Nutter unmap_mapping_range(ctx->signal1, 0, 0x4000, 1); 1156df10a82SMark Nutter if (ctx->signal2) 1166df10a82SMark Nutter unmap_mapping_range(ctx->signal2, 0, 0x4000, 1); 1178b3d6663SArnd Bergmann } 1188b3d6663SArnd Bergmann 1198b3d6663SArnd Bergmann int spu_acquire_runnable(struct spu_context *ctx) 1208b3d6663SArnd Bergmann { 1218b3d6663SArnd Bergmann int ret = 0; 1228b3d6663SArnd Bergmann 1238b3d6663SArnd Bergmann down_read(&ctx->state_sema); 1242a911f0bSArnd Bergmann if (ctx->state == SPU_STATE_RUNNABLE) { 1252a911f0bSArnd Bergmann ctx->spu->prio = current->prio; 1268b3d6663SArnd Bergmann return 0; 1272a911f0bSArnd Bergmann } 128762cf6daSArnd Bergmann up_read(&ctx->state_sema); 129762cf6daSArnd Bergmann 130762cf6daSArnd Bergmann down_write(&ctx->state_sema); 1318b3d6663SArnd Bergmann /* ctx is about to be freed, can't acquire any more */ 1328b3d6663SArnd Bergmann if (!ctx->owner) { 1338b3d6663SArnd Bergmann ret = -EINVAL; 1348b3d6663SArnd Bergmann goto out; 1358b3d6663SArnd Bergmann } 1368b3d6663SArnd Bergmann 1378b3d6663SArnd Bergmann if (ctx->state == SPU_STATE_SAVED) { 1388b3d6663SArnd Bergmann ret = spu_activate(ctx, 0); 1398b3d6663SArnd Bergmann if (ret) 1408b3d6663SArnd Bergmann goto out; 14101062465SArnd Bergmann ctx->state = SPU_STATE_RUNNABLE; 14201062465SArnd Bergmann } 1438b3d6663SArnd Bergmann 144762cf6daSArnd Bergmann downgrade_write(&ctx->state_sema); 1458b3d6663SArnd Bergmann /* On success, we return holding the lock */ 146762cf6daSArnd Bergmann 1478b3d6663SArnd Bergmann return ret; 1488b3d6663SArnd Bergmann out: 1498b3d6663SArnd Bergmann /* Release here, to simplify calling code. */ 150762cf6daSArnd Bergmann up_write(&ctx->state_sema); 1518b3d6663SArnd Bergmann 1528b3d6663SArnd Bergmann return ret; 1538b3d6663SArnd Bergmann } 1548b3d6663SArnd Bergmann 1558b3d6663SArnd Bergmann void spu_acquire_saved(struct spu_context *ctx) 1568b3d6663SArnd Bergmann { 1578b3d6663SArnd Bergmann down_read(&ctx->state_sema); 1588b3d6663SArnd Bergmann 1598b3d6663SArnd Bergmann if (ctx->state == SPU_STATE_SAVED) 1608b3d6663SArnd Bergmann return; 1618b3d6663SArnd Bergmann 1628b3d6663SArnd Bergmann up_read(&ctx->state_sema); 1638b3d6663SArnd Bergmann down_write(&ctx->state_sema); 1648b3d6663SArnd Bergmann 1658b3d6663SArnd Bergmann if (ctx->state == SPU_STATE_RUNNABLE) { 1668b3d6663SArnd Bergmann spu_deactivate(ctx); 1678b3d6663SArnd Bergmann ctx->state = SPU_STATE_SAVED; 1688b3d6663SArnd Bergmann } 1698b3d6663SArnd Bergmann 1708b3d6663SArnd Bergmann downgrade_write(&ctx->state_sema); 1718b3d6663SArnd Bergmann } 172