Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6-block.git] / arch / powerpc / platforms / cell / spufs / run.c
CommitLineData
0afacde3 1#define DEBUG
2
ce8ab854
AB
3#include <linux/wait.h>
4#include <linux/ptrace.h>
5
6#include <asm/spu.h>
c6730ed4
JK
7#include <asm/spu_priv1.h>
8#include <asm/io.h>
cfff5b23 9#include <asm/unistd.h>
ce8ab854
AB
10
11#include "spufs.h"
12
13/* interrupt-level stop callback function. */
14void spufs_stop_callback(struct spu *spu)
15{
16 struct spu_context *ctx = spu->ctx;
17
d6ad39bc
JK
18 /*
19 * It should be impossible to preempt a context while an exception
20 * is being processed, since the context switch code is specially
21 * coded to deal with interrupts ... But, just in case, sanity check
22 * the context pointer. It is OK to return doing nothing since
23 * the exception will be regenerated when the context is resumed.
24 */
25 if (ctx) {
26 /* Copy exception arguments into module specific structure */
27 ctx->csa.class_0_pending = spu->class_0_pending;
28 ctx->csa.dsisr = spu->dsisr;
29 ctx->csa.dar = spu->dar;
30
31 /* ensure that the exception status has hit memory before a
32 * thread waiting on the context's stop queue is woken */
33 smp_wmb();
34
35 wake_up_all(&ctx->stop_wq);
36 }
37
38 /* Clear callback arguments from spu structure */
39 spu->class_0_pending = 0;
40 spu->dsisr = 0;
41 spu->dar = 0;
ce8ab854
AB
42}
43
e65c2f6f 44int spu_stopped(struct spu_context *ctx, u32 *stat)
ce8ab854 45{
e65c2f6f
LB
46 u64 dsisr;
47 u32 stopped;
ce8ab854
AB
48
49 *stat = ctx->ops->status_read(ctx);
36aaccc1 50
e65c2f6f
LB
51 if (test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags))
52 return 1;
53
54 stopped = SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
55 SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
85687ff2 56 if (!(*stat & SPU_STATUS_RUNNING) && (*stat & stopped))
e65c2f6f
LB
57 return 1;
58
59 dsisr = ctx->csa.dsisr;
60 if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED))
36aaccc1 61 return 1;
e65c2f6f
LB
62
63 if (ctx->csa.class_0_pending)
64 return 1;
65
66 return 0;
ce8ab854
AB
67}
68
c6730ed4
JK
69static int spu_setup_isolated(struct spu_context *ctx)
70{
71 int ret;
72 u64 __iomem *mfc_cntl;
73 u64 sr1;
74 u32 status;
75 unsigned long timeout;
76 const u32 status_loading = SPU_STATUS_RUNNING
77 | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
78
7ec18ab9 79 ret = -ENODEV;
c6730ed4 80 if (!isolated_loader)
c6730ed4
JK
81 goto out;
82
7ec18ab9
CH
83 /*
84 * We need to exclude userspace access to the context.
85 *
86 * To protect against memory access we invalidate all ptes
87 * and make sure the pagefault handlers block on the mutex.
88 */
89 spu_unmap_mappings(ctx);
90
c6730ed4
JK
91 mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
92
93 /* purge the MFC DMA queue to ensure no spurious accesses before we
94 * enter kernel mode */
95 timeout = jiffies + HZ;
96 out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
97 while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
98 != MFC_CNTL_PURGE_DMA_COMPLETE) {
99 if (time_after(jiffies, timeout)) {
100 printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
e48b1b45 101 __func__);
c6730ed4 102 ret = -EIO;
7ec18ab9 103 goto out;
c6730ed4
JK
104 }
105 cond_resched();
106 }
107
108 /* put the SPE in kernel mode to allow access to the loader */
109 sr1 = spu_mfc_sr1_get(ctx->spu);
110 sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
111 spu_mfc_sr1_set(ctx->spu, sr1);
112
113 /* start the loader */
114 ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
115 ctx->ops->signal2_write(ctx,
116 (unsigned long)isolated_loader & 0xffffffff);
117
118 ctx->ops->runcntl_write(ctx,
119 SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
120
121 ret = 0;
122 timeout = jiffies + HZ;
123 while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
124 status_loading) {
125 if (time_after(jiffies, timeout)) {
126 printk(KERN_ERR "%s: timeout waiting for loader\n",
e48b1b45 127 __func__);
c6730ed4
JK
128 ret = -EIO;
129 goto out_drop_priv;
130 }
131 cond_resched();
132 }
133
134 if (!(status & SPU_STATUS_RUNNING)) {
135 /* If isolated LOAD has failed: run SPU, we will get a stop-and
136 * signal later. */
e48b1b45 137 pr_debug("%s: isolated LOAD failed\n", __func__);
c6730ed4
JK
138 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
139 ret = -EACCES;
7ec18ab9
CH
140 goto out_drop_priv;
141 }
c6730ed4 142
7ec18ab9 143 if (!(status & SPU_STATUS_ISOLATED_STATE)) {
c6730ed4 144 /* This isn't allowed by the CBEA, but check anyway */
e48b1b45 145 pr_debug("%s: SPU fell out of isolated mode?\n", __func__);
c6730ed4
JK
146 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
147 ret = -EINVAL;
7ec18ab9 148 goto out_drop_priv;
c6730ed4
JK
149 }
150
151out_drop_priv:
152 /* Finished accessing the loader. Drop kernel mode */
153 sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
154 spu_mfc_sr1_set(ctx->spu, sr1);
155
c6730ed4
JK
156out:
157 return ret;
158}
159
36aaccc1 160static int spu_run_init(struct spu_context *ctx, u32 *npc)
ce8ab854 161{
e65c2f6f 162 unsigned long runcntl = SPU_RUNCNTL_RUNNABLE;
91569531 163 int ret;
cc210b3e 164
27ec41d3
AD
165 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
166
e65c2f6f
LB
167 /*
168 * NOSCHED is synchronous scheduling with respect to the caller.
169 * The caller waits for the context to be loaded.
170 */
171 if (ctx->flags & SPU_CREATE_NOSCHED) {
91569531 172 if (ctx->state == SPU_STATE_SAVED) {
91569531
LB
173 ret = spu_activate(ctx, 0);
174 if (ret)
175 return ret;
176 }
e65c2f6f 177 }
aa45e256 178
e65c2f6f
LB
179 /*
180 * Apply special setup as required.
181 */
182 if (ctx->flags & SPU_CREATE_ISOLATE) {
c6730ed4 183 if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
91569531 184 ret = spu_setup_isolated(ctx);
7ec18ab9 185 if (ret)
aa45e256 186 return ret;
c6730ed4
JK
187 }
188
91569531
LB
189 /*
190 * If userspace has set the runcntrl register (eg, to
191 * issue an isolated exit), we need to re-set it here
192 */
c6730ed4
JK
193 runcntl = ctx->ops->runcntl_read(ctx) &
194 (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
195 if (runcntl == 0)
196 runcntl = SPU_RUNCNTL_RUNNABLE;
e65c2f6f 197 }
91569531 198
e65c2f6f 199 if (ctx->flags & SPU_CREATE_NOSCHED) {
91569531
LB
200 spuctx_switch_state(ctx, SPU_UTIL_USER);
201 ctx->ops->runcntl_write(ctx, runcntl);
2eb1b120 202 } else {
cc210b3e
LB
203 unsigned long privcntl;
204
05169237 205 if (test_thread_flag(TIF_SINGLESTEP))
cc210b3e
LB
206 privcntl = SPU_PRIVCNTL_MODE_SINGLE_STEP;
207 else
208 privcntl = SPU_PRIVCNTL_MODE_NORMAL;
cc210b3e
LB
209
210 ctx->ops->npc_write(ctx, *npc);
211 ctx->ops->privcntl_write(ctx, privcntl);
e65c2f6f 212 ctx->ops->runcntl_write(ctx, runcntl);
c6730ed4 213
91569531 214 if (ctx->state == SPU_STATE_SAVED) {
91569531
LB
215 ret = spu_activate(ctx, 0);
216 if (ret)
217 return ret;
e65c2f6f
LB
218 } else {
219 spuctx_switch_state(ctx, SPU_UTIL_USER);
91569531 220 }
91569531 221 }
27ec41d3 222
ce7c191b 223 set_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags);
aa45e256 224 return 0;
ce8ab854
AB
225}
226
36aaccc1
BN
227static int spu_run_fini(struct spu_context *ctx, u32 *npc,
228 u32 *status)
ce8ab854
AB
229{
230 int ret = 0;
231
e65c2f6f
LB
232 spu_del_from_rq(ctx);
233
ce8ab854
AB
234 *status = ctx->ops->status_read(ctx);
235 *npc = ctx->ops->npc_read(ctx);
27ec41d3
AD
236
237 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
ce7c191b 238 clear_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags);
ce8ab854
AB
239 spu_release(ctx);
240
241 if (signal_pending(current))
242 ret = -ERESTARTSYS;
2ebb2477 243
ce8ab854
AB
244 return ret;
245}
246
2dd14934
AB
247/*
248 * SPU syscall restarting is tricky because we violate the basic
249 * assumption that the signal handler is running on the interrupted
250 * thread. Here instead, the handler runs on PowerPC user space code,
251 * while the syscall was called from the SPU.
252 * This means we can only do a very rough approximation of POSIX
253 * signal semantics.
254 */
1238819a 255static int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
2dd14934
AB
256 unsigned int *npc)
257{
258 int ret;
259
260 switch (*spu_ret) {
261 case -ERESTARTSYS:
262 case -ERESTARTNOINTR:
263 /*
264 * Enter the regular syscall restarting for
265 * sys_spu_run, then restart the SPU syscall
266 * callback.
267 */
268 *npc -= 8;
269 ret = -ERESTARTSYS;
270 break;
271 case -ERESTARTNOHAND:
272 case -ERESTART_RESTARTBLOCK:
273 /*
274 * Restart block is too hard for now, just return -EINTR
275 * to the SPU.
276 * ERESTARTNOHAND comes from sys_pause, we also return
277 * -EINTR from there.
278 * Assume that we need to be restarted ourselves though.
279 */
280 *spu_ret = -EINTR;
281 ret = -ERESTARTSYS;
282 break;
283 default:
284 printk(KERN_WARNING "%s: unexpected return code %ld\n",
e48b1b45 285 __func__, *spu_ret);
2dd14934
AB
286 ret = 0;
287 }
288 return ret;
289}
290
1238819a 291static int spu_process_callback(struct spu_context *ctx)
2dd14934
AB
292{
293 struct spu_syscall_block s;
294 u32 ls_pointer, npc;
9e2fe2ce 295 void __iomem *ls;
2dd14934 296 long spu_ret;
c9101bdb 297 int ret, ret2;
2dd14934
AB
298
299 /* get syscall block from local store */
9e2fe2ce
AM
300 npc = ctx->ops->npc_read(ctx) & ~3;
301 ls = (void __iomem *)ctx->ops->get_ls(ctx);
302 ls_pointer = in_be32(ls + npc);
2dd14934
AB
303 if (ls_pointer > (LS_SIZE - sizeof(s)))
304 return -EFAULT;
9e2fe2ce 305 memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
2dd14934
AB
306
307 /* do actual syscall without pinning the spu */
308 ret = 0;
309 spu_ret = -ENOSYS;
310 npc += 4;
311
312 if (s.nr_ret < __NR_syscalls) {
313 spu_release(ctx);
314 /* do actual system call from here */
315 spu_ret = spu_sys_callback(&s);
316 if (spu_ret <= -ERESTARTSYS) {
317 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
318 }
c9101bdb 319 ret2 = spu_acquire(ctx);
2dd14934
AB
320 if (ret == -ERESTARTSYS)
321 return ret;
c9101bdb
CH
322 if (ret2)
323 return -EINTR;
2dd14934
AB
324 }
325
4eb5aef5
JK
326 /* need to re-get the ls, as it may have changed when we released the
327 * spu */
328 ls = (void __iomem *)ctx->ops->get_ls(ctx);
329
2dd14934 330 /* write result, jump over indirect pointer */
9e2fe2ce 331 memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
2dd14934
AB
332 ctx->ops->npc_write(ctx, npc);
333 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
334 return ret;
335}
336
50af32a9 337long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event)
ce8ab854
AB
338{
339 int ret;
36aaccc1 340 struct spu *spu;
9add11da 341 u32 status;
ce8ab854 342
e45d48a3 343 if (mutex_lock_interruptible(&ctx->run_mutex))
ce8ab854
AB
344 return -ERESTARTSYS;
345
c25620d7 346 spu_enable_spu(ctx);
9add11da 347 ctx->event_return = 0;
aa45e256 348
c9101bdb
CH
349 ret = spu_acquire(ctx);
350 if (ret)
351 goto out_unlock;
2cf2b3b4 352
91569531 353 spu_update_sched_info(ctx);
aa45e256
CH
354
355 ret = spu_run_init(ctx, npc);
356 if (ret) {
357 spu_release(ctx);
ce8ab854 358 goto out;
aa45e256 359 }
ce8ab854
AB
360
361 do {
9add11da 362 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
eebead5b
CH
363 if (unlikely(ret)) {
364 /*
365 * This is nasty: we need the state_mutex for all the
366 * bookkeeping even if the syscall was interrupted by
367 * a signal. ewww.
368 */
369 mutex_lock(&ctx->state_mutex);
ce8ab854 370 break;
eebead5b 371 }
36aaccc1
BN
372 spu = ctx->spu;
373 if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE,
374 &ctx->sched_flags))) {
375 if (!(status & SPU_STATUS_STOPPED_BY_STOP)) {
376 spu_switch_notify(spu, ctx);
377 continue;
378 }
379 }
27ec41d3
AD
380
381 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
382
9add11da
AB
383 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
384 (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
2dd14934
AB
385 ret = spu_process_callback(ctx);
386 if (ret)
387 break;
9add11da 388 status &= ~SPU_STATUS_STOPPED_BY_STOP;
2dd14934 389 }
57dace23
AB
390 ret = spufs_handle_class1(ctx);
391 if (ret)
392 break;
393
d6ad39bc
JK
394 ret = spufs_handle_class0(ctx);
395 if (ret)
396 break;
397
d6ad39bc
JK
398 if (signal_pending(current))
399 ret = -ERESTARTSYS;
9add11da 400 } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
05169237
BH
401 SPU_STATUS_STOPPED_BY_HALT |
402 SPU_STATUS_SINGLE_STEP)));
ce8ab854 403
c25620d7 404 spu_disable_spu(ctx);
9add11da 405 ret = spu_run_fini(ctx, npc, &status);
ce8ab854
AB
406 spu_yield(ctx);
407
5158e9b5
CH
408 spu_switch_log_notify(NULL, ctx, SWITCH_LOG_EXIT, status);
409
e66686b4
LB
410 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
411 (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100))
412 ctx->stats.libassist++;
413
2ebb2477
MN
414 if ((ret == 0) ||
415 ((ret == -ERESTARTSYS) &&
416 ((status & SPU_STATUS_STOPPED_BY_HALT) ||
05169237 417 (status & SPU_STATUS_SINGLE_STEP) ||
2ebb2477
MN
418 ((status & SPU_STATUS_STOPPED_BY_STOP) &&
419 (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
420 ret = status;
421
05169237
BH
422 /* Note: we don't need to force_sig SIGTRAP on single-step
423 * since we have TIF_SINGLESTEP set, thus the kernel will do
424 * it upon return from the syscall anyawy
425 */
60cf54db
JK
426 if (unlikely(status & SPU_STATUS_SINGLE_STEP))
427 ret = -ERESTARTSYS;
428
429 else if (unlikely((status & SPU_STATUS_STOPPED_BY_STOP)
430 && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff)) {
c2b2226c
AB
431 force_sig(SIGTRAP, current);
432 ret = -ERESTARTSYS;
2ebb2477
MN
433 }
434
ce8ab854 435out:
9add11da 436 *event = ctx->event_return;
c9101bdb 437out_unlock:
e45d48a3 438 mutex_unlock(&ctx->run_mutex);
ce8ab854
AB
439 return ret;
440}