[PATCH] spufs: The SPU file system, base
[pandora-kernel.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 "spufs.h"
26
27 struct spu_context *alloc_spu_context(void)
28 {
29         struct spu_context *ctx;
30         ctx = kmalloc(sizeof *ctx, GFP_KERNEL);
31         if (!ctx)
32                 goto out;
33         ctx->spu = spu_alloc();
34         if (!ctx->spu)
35                 goto out_free;
36         init_rwsem(&ctx->backing_sema);
37         spin_lock_init(&ctx->mmio_lock);
38         kref_init(&ctx->kref);
39         goto out;
40 out_free:
41         kfree(ctx);
42         ctx = NULL;
43 out:
44         return ctx;
45 }
46
47 void destroy_spu_context(struct kref *kref)
48 {
49         struct spu_context *ctx;
50         ctx = container_of(kref, struct spu_context, kref);
51         if (ctx->spu)
52                 spu_free(ctx->spu);
53         kfree(ctx);
54 }
55
56 struct spu_context * get_spu_context(struct spu_context *ctx)
57 {
58         kref_get(&ctx->kref);
59         return ctx;
60 }
61
62 int put_spu_context(struct spu_context *ctx)
63 {
64         return kref_put(&ctx->kref, &destroy_spu_context);
65 }
66
67