Path: blob/master/arch/powerpc/platforms/cell/spufs/context.c
26498 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* SPU file system -- SPU context management3*4* (C) Copyright IBM Deutschland Entwicklung GmbH 20055*6* Author: Arnd Bergmann <[email protected]>7*/89#include <linux/fs.h>10#include <linux/mm.h>11#include <linux/slab.h>12#include <linux/atomic.h>13#include <linux/sched.h>14#include <linux/sched/mm.h>1516#include <asm/spu.h>17#include <asm/spu_csa.h>18#include "spufs.h"19#include "sputrace.h"202122atomic_t nr_spu_contexts = ATOMIC_INIT(0);2324struct spu_context *alloc_spu_context(struct spu_gang *gang)25{26struct spu_context *ctx;2728ctx = kzalloc(sizeof *ctx, GFP_KERNEL);29if (!ctx)30goto out;31/* Binding to physical processor deferred32* until spu_activate().33*/34if (spu_init_csa(&ctx->csa))35goto out_free;36spin_lock_init(&ctx->mmio_lock);37mutex_init(&ctx->mapping_lock);38kref_init(&ctx->kref);39mutex_init(&ctx->state_mutex);40mutex_init(&ctx->run_mutex);41init_waitqueue_head(&ctx->ibox_wq);42init_waitqueue_head(&ctx->wbox_wq);43init_waitqueue_head(&ctx->stop_wq);44init_waitqueue_head(&ctx->mfc_wq);45init_waitqueue_head(&ctx->run_wq);46ctx->state = SPU_STATE_SAVED;47ctx->ops = &spu_backing_ops;48ctx->owner = get_task_mm(current);49INIT_LIST_HEAD(&ctx->rq);50INIT_LIST_HEAD(&ctx->aff_list);51if (gang)52spu_gang_add_ctx(gang, ctx);5354__spu_update_sched_info(ctx);55spu_set_timeslice(ctx);56ctx->stats.util_state = SPU_UTIL_IDLE_LOADED;57ctx->stats.tstamp = ktime_get_ns();5859atomic_inc(&nr_spu_contexts);60goto out;61out_free:62kfree(ctx);63ctx = NULL;64out:65return ctx;66}6768void destroy_spu_context(struct kref *kref)69{70struct spu_context *ctx;71ctx = container_of(kref, struct spu_context, kref);72spu_context_nospu_trace(destroy_spu_context__enter, ctx);73mutex_lock(&ctx->state_mutex);74spu_deactivate(ctx);75mutex_unlock(&ctx->state_mutex);76spu_fini_csa(&ctx->csa);77if (ctx->gang)78spu_gang_remove_ctx(ctx->gang, ctx);79if (ctx->prof_priv_kref)80kref_put(ctx->prof_priv_kref, ctx->prof_priv_release);81BUG_ON(!list_empty(&ctx->rq));82atomic_dec(&nr_spu_contexts);83kfree(ctx->switch_log);84kfree(ctx);85}8687struct spu_context * get_spu_context(struct spu_context *ctx)88{89kref_get(&ctx->kref);90return ctx;91}9293int put_spu_context(struct spu_context *ctx)94{95return kref_put(&ctx->kref, &destroy_spu_context);96}9798/* give up the mm reference when the context is about to be destroyed */99void spu_forget(struct spu_context *ctx)100{101struct mm_struct *mm;102103/*104* This is basically an open-coded spu_acquire_saved, except that105* we don't acquire the state mutex interruptible, and we don't106* want this context to be rescheduled on release.107*/108mutex_lock(&ctx->state_mutex);109if (ctx->state != SPU_STATE_SAVED)110spu_deactivate(ctx);111112mm = ctx->owner;113ctx->owner = NULL;114mmput(mm);115spu_release(ctx);116}117118void spu_unmap_mappings(struct spu_context *ctx)119{120mutex_lock(&ctx->mapping_lock);121if (ctx->local_store)122unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1);123if (ctx->mfc)124unmap_mapping_range(ctx->mfc, 0, SPUFS_MFC_MAP_SIZE, 1);125if (ctx->cntl)126unmap_mapping_range(ctx->cntl, 0, SPUFS_CNTL_MAP_SIZE, 1);127if (ctx->signal1)128unmap_mapping_range(ctx->signal1, 0, SPUFS_SIGNAL_MAP_SIZE, 1);129if (ctx->signal2)130unmap_mapping_range(ctx->signal2, 0, SPUFS_SIGNAL_MAP_SIZE, 1);131if (ctx->mss)132unmap_mapping_range(ctx->mss, 0, SPUFS_MSS_MAP_SIZE, 1);133if (ctx->psmap)134unmap_mapping_range(ctx->psmap, 0, SPUFS_PS_MAP_SIZE, 1);135mutex_unlock(&ctx->mapping_lock);136}137138/**139* spu_acquire_saved - lock spu contex and make sure it is in saved state140* @ctx: spu contex to lock141*/142int spu_acquire_saved(struct spu_context *ctx)143{144int ret;145146spu_context_nospu_trace(spu_acquire_saved__enter, ctx);147148ret = spu_acquire(ctx);149if (ret)150return ret;151152if (ctx->state != SPU_STATE_SAVED) {153set_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags);154spu_deactivate(ctx);155}156157return 0;158}159160/**161* spu_release_saved - unlock spu context and return it to the runqueue162* @ctx: context to unlock163*/164void spu_release_saved(struct spu_context *ctx)165{166BUG_ON(ctx->state != SPU_STATE_SAVED);167168if (test_and_clear_bit(SPU_SCHED_WAS_ACTIVE, &ctx->sched_flags) &&169test_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags))170spu_activate(ctx, 0);171172spu_release(ctx);173}174175176177