[PATCH] spufs: switchable spu contexts
[powerpc.git] / arch / powerpc / platforms / cell / spufs / context.c
1 /*
2  * SPU file system -- SPU context management
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *
6  * Author: Arnd Bergmann <arndb@de.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/slab.h>
24 #include <asm/spu.h>
25 #include <asm/spu_csa.h>
26 #include "spufs.h"
27
28 struct spu_context *alloc_spu_context(void)
29 {
30         struct spu_context *ctx;
31         ctx = kmalloc(sizeof *ctx, GFP_KERNEL);
32         if (!ctx)
33                 goto out;
34         /* Future enhancement: do not call spu_alloc()
35          * here.  This step should be deferred until
36          * spu_run()!!
37          *
38          * More work needs to be done to read(),
39          * write(), mmap(), etc., so that operations
40          * are performed on CSA when the context is
41          * not currently being run.  In this way we
42          * can support arbitrarily large number of
43          * entries in /spu, allow state queries, etc.
44          */
45         ctx->spu = spu_alloc();
46         if (!ctx->spu)
47                 goto out_free;
48         spu_init_csa(&ctx->csa);
49         if (!ctx->csa.lscsa) {
50                 spu_free(ctx->spu);
51                 goto out_free;
52         }
53         init_rwsem(&ctx->backing_sema);
54         spin_lock_init(&ctx->mmio_lock);
55         kref_init(&ctx->kref);
56         goto out;
57 out_free:
58         kfree(ctx);
59         ctx = NULL;
60 out:
61         return ctx;
62 }
63
64 void destroy_spu_context(struct kref *kref)
65 {
66         struct spu_context *ctx;
67         ctx = container_of(kref, struct spu_context, kref);
68         if (ctx->spu)
69                 spu_free(ctx->spu);
70         spu_fini_csa(&ctx->csa);
71         kfree(ctx);
72 }
73
74 struct spu_context * get_spu_context(struct spu_context *ctx)
75 {
76         kref_get(&ctx->kref);
77         return ctx;
78 }
79
80 int put_spu_context(struct spu_context *ctx)
81 {
82         return kref_put(&ctx->kref, &destroy_spu_context);
83 }
84
85