Commit | Line | Data |
---|---|---|
67207b96 AB |
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 | ||
8b3d6663 AB |
23 | #include <linux/fs.h> |
24 | #include <linux/mm.h> | |
67207b96 AB |
25 | #include <linux/slab.h> |
26 | #include <asm/spu.h> | |
5473af04 | 27 | #include <asm/spu_csa.h> |
67207b96 AB |
28 | #include "spufs.h" |
29 | ||
8b3d6663 | 30 | struct spu_context *alloc_spu_context(struct address_space *local_store) |
67207b96 AB |
31 | { |
32 | struct spu_context *ctx; | |
33 | ctx = kmalloc(sizeof *ctx, GFP_KERNEL); | |
34 | if (!ctx) | |
35 | goto out; | |
8b3d6663 AB |
36 | /* Binding to physical processor deferred |
37 | * until spu_activate(). | |
5473af04 | 38 | */ |
5473af04 MN |
39 | spu_init_csa(&ctx->csa); |
40 | if (!ctx->csa.lscsa) { | |
5473af04 MN |
41 | goto out_free; |
42 | } | |
67207b96 AB |
43 | spin_lock_init(&ctx->mmio_lock); |
44 | kref_init(&ctx->kref); | |
8b3d6663 AB |
45 | init_rwsem(&ctx->state_sema); |
46 | init_waitqueue_head(&ctx->ibox_wq); | |
47 | init_waitqueue_head(&ctx->wbox_wq); | |
48 | ctx->ibox_fasync = NULL; | |
49 | ctx->wbox_fasync = NULL; | |
50 | ctx->state = SPU_STATE_SAVED; | |
51 | ctx->local_store = local_store; | |
52 | ctx->spu = NULL; | |
53 | ctx->ops = &spu_backing_ops; | |
54 | ctx->owner = get_task_mm(current); | |
67207b96 AB |
55 | goto out; |
56 | out_free: | |
57 | kfree(ctx); | |
58 | ctx = NULL; | |
59 | out: | |
60 | return ctx; | |
61 | } | |
62 | ||
63 | void destroy_spu_context(struct kref *kref) | |
64 | { | |
65 | struct spu_context *ctx; | |
66 | ctx = container_of(kref, struct spu_context, kref); | |
8b3d6663 AB |
67 | down_write(&ctx->state_sema); |
68 | spu_deactivate(ctx); | |
69 | ctx->ibox_fasync = NULL; | |
70 | ctx->wbox_fasync = NULL; | |
71 | up_write(&ctx->state_sema); | |
5473af04 | 72 | spu_fini_csa(&ctx->csa); |
67207b96 AB |
73 | kfree(ctx); |
74 | } | |
75 | ||
76 | struct spu_context * get_spu_context(struct spu_context *ctx) | |
77 | { | |
78 | kref_get(&ctx->kref); | |
79 | return ctx; | |
80 | } | |
81 | ||
82 | int put_spu_context(struct spu_context *ctx) | |
83 | { | |
84 | return kref_put(&ctx->kref, &destroy_spu_context); | |
85 | } | |
86 | ||
8b3d6663 AB |
87 | /* give up the mm reference when the context is about to be destroyed */ |
88 | void spu_forget(struct spu_context *ctx) | |
89 | { | |
90 | struct mm_struct *mm; | |
91 | spu_acquire_saved(ctx); | |
92 | mm = ctx->owner; | |
93 | ctx->owner = NULL; | |
94 | mmput(mm); | |
95 | spu_release(ctx); | |
96 | } | |
97 | ||
98 | void spu_acquire(struct spu_context *ctx) | |
99 | { | |
100 | down_read(&ctx->state_sema); | |
101 | } | |
102 | ||
103 | void spu_release(struct spu_context *ctx) | |
104 | { | |
105 | up_read(&ctx->state_sema); | |
106 | } | |
107 | ||
108 | static void spu_unmap_mappings(struct spu_context *ctx) | |
109 | { | |
110 | unmap_mapping_range(ctx->local_store, 0, LS_SIZE, 1); | |
111 | } | |
112 | ||
113 | int spu_acquire_runnable(struct spu_context *ctx) | |
114 | { | |
115 | int ret = 0; | |
67207b96 | 116 | |
8b3d6663 AB |
117 | down_read(&ctx->state_sema); |
118 | if (ctx->state == SPU_STATE_RUNNABLE) | |
119 | return 0; | |
120 | /* ctx is about to be freed, can't acquire any more */ | |
121 | if (!ctx->owner) { | |
122 | ret = -EINVAL; | |
123 | goto out; | |
124 | } | |
125 | up_read(&ctx->state_sema); | |
126 | ||
127 | down_write(&ctx->state_sema); | |
128 | if (ctx->state == SPU_STATE_SAVED) { | |
129 | spu_unmap_mappings(ctx); | |
130 | ret = spu_activate(ctx, 0); | |
131 | ctx->state = SPU_STATE_RUNNABLE; | |
132 | } | |
133 | downgrade_write(&ctx->state_sema); | |
134 | if (ret) | |
135 | goto out; | |
136 | ||
137 | /* On success, we return holding the lock */ | |
138 | return ret; | |
139 | out: | |
140 | /* Release here, to simplify calling code. */ | |
141 | up_read(&ctx->state_sema); | |
142 | ||
143 | return ret; | |
144 | } | |
145 | ||
146 | void spu_acquire_saved(struct spu_context *ctx) | |
147 | { | |
148 | down_read(&ctx->state_sema); | |
149 | ||
150 | if (ctx->state == SPU_STATE_SAVED) | |
151 | return; | |
152 | ||
153 | up_read(&ctx->state_sema); | |
154 | down_write(&ctx->state_sema); | |
155 | ||
156 | if (ctx->state == SPU_STATE_RUNNABLE) { | |
157 | spu_unmap_mappings(ctx); | |
158 | spu_deactivate(ctx); | |
159 | ctx->state = SPU_STATE_SAVED; | |
160 | } | |
161 | ||
162 | downgrade_write(&ctx->state_sema); | |
163 | } |