[POWERPC] spufs: reorganize spu_run_init
[linux-2.6/x86.git] / arch / powerpc / platforms / cell / spufs / run.c
blob652ae1366dc8cc1eb29075099ac5ff756715ff07
1 #define DEBUG
3 #include <linux/wait.h>
4 #include <linux/ptrace.h>
6 #include <asm/spu.h>
7 #include <asm/spu_priv1.h>
8 #include <asm/io.h>
9 #include <asm/unistd.h>
11 #include "spufs.h"
13 /* interrupt-level stop callback function. */
14 void spufs_stop_callback(struct spu *spu)
16 struct spu_context *ctx = spu->ctx;
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.
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;
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();
35 wake_up_all(&ctx->stop_wq);
38 /* Clear callback arguments from spu structure */
39 spu->class_0_pending = 0;
40 spu->dsisr = 0;
41 spu->dar = 0;
44 static inline int spu_stopped(struct spu_context *ctx, u32 *stat)
46 struct spu *spu;
47 u64 pte_fault;
49 *stat = ctx->ops->status_read(ctx);
51 spu = ctx->spu;
52 if (ctx->state != SPU_STATE_RUNNABLE ||
53 test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags))
54 return 1;
55 pte_fault = ctx->csa.dsisr &
56 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
57 return (!(*stat & SPU_STATUS_RUNNING) || pte_fault || ctx->csa.class_0_pending) ?
58 1 : 0;
61 static int spu_setup_isolated(struct spu_context *ctx)
63 int ret;
64 u64 __iomem *mfc_cntl;
65 u64 sr1;
66 u32 status;
67 unsigned long timeout;
68 const u32 status_loading = SPU_STATUS_RUNNING
69 | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
71 ret = -ENODEV;
72 if (!isolated_loader)
73 goto out;
76 * We need to exclude userspace access to the context.
78 * To protect against memory access we invalidate all ptes
79 * and make sure the pagefault handlers block on the mutex.
81 spu_unmap_mappings(ctx);
83 mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
85 /* purge the MFC DMA queue to ensure no spurious accesses before we
86 * enter kernel mode */
87 timeout = jiffies + HZ;
88 out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
89 while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
90 != MFC_CNTL_PURGE_DMA_COMPLETE) {
91 if (time_after(jiffies, timeout)) {
92 printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
93 __FUNCTION__);
94 ret = -EIO;
95 goto out;
97 cond_resched();
100 /* put the SPE in kernel mode to allow access to the loader */
101 sr1 = spu_mfc_sr1_get(ctx->spu);
102 sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
103 spu_mfc_sr1_set(ctx->spu, sr1);
105 /* start the loader */
106 ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
107 ctx->ops->signal2_write(ctx,
108 (unsigned long)isolated_loader & 0xffffffff);
110 ctx->ops->runcntl_write(ctx,
111 SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
113 ret = 0;
114 timeout = jiffies + HZ;
115 while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
116 status_loading) {
117 if (time_after(jiffies, timeout)) {
118 printk(KERN_ERR "%s: timeout waiting for loader\n",
119 __FUNCTION__);
120 ret = -EIO;
121 goto out_drop_priv;
123 cond_resched();
126 if (!(status & SPU_STATUS_RUNNING)) {
127 /* If isolated LOAD has failed: run SPU, we will get a stop-and
128 * signal later. */
129 pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
130 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
131 ret = -EACCES;
132 goto out_drop_priv;
135 if (!(status & SPU_STATUS_ISOLATED_STATE)) {
136 /* This isn't allowed by the CBEA, but check anyway */
137 pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
138 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
139 ret = -EINVAL;
140 goto out_drop_priv;
143 out_drop_priv:
144 /* Finished accessing the loader. Drop kernel mode */
145 sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
146 spu_mfc_sr1_set(ctx->spu, sr1);
148 out:
149 return ret;
152 static int spu_run_init(struct spu_context *ctx, u32 *npc)
154 unsigned long runcntl;
155 int ret;
157 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
159 if (ctx->flags & SPU_CREATE_ISOLATE) {
161 * Force activation of spu. Isolated state assumes that
162 * special loader context is loaded and running on spu.
164 if (ctx->state == SPU_STATE_SAVED) {
165 spu_set_timeslice(ctx);
167 ret = spu_activate(ctx, 0);
168 if (ret)
169 return ret;
172 if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
173 ret = spu_setup_isolated(ctx);
174 if (ret)
175 return ret;
179 * If userspace has set the runcntrl register (eg, to
180 * issue an isolated exit), we need to re-set it here
182 runcntl = ctx->ops->runcntl_read(ctx) &
183 (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
184 if (runcntl == 0)
185 runcntl = SPU_RUNCNTL_RUNNABLE;
187 spuctx_switch_state(ctx, SPU_UTIL_USER);
188 ctx->ops->runcntl_write(ctx, runcntl);
190 } else {
191 unsigned long privcntl;
193 if (test_thread_flag(TIF_SINGLESTEP))
194 privcntl = SPU_PRIVCNTL_MODE_SINGLE_STEP;
195 else
196 privcntl = SPU_PRIVCNTL_MODE_NORMAL;
197 runcntl = SPU_RUNCNTL_RUNNABLE;
199 ctx->ops->npc_write(ctx, *npc);
200 ctx->ops->privcntl_write(ctx, privcntl);
202 if (ctx->state == SPU_STATE_SAVED) {
203 spu_set_timeslice(ctx);
204 ret = spu_activate(ctx, 0);
205 if (ret)
206 return ret;
209 spuctx_switch_state(ctx, SPU_UTIL_USER);
210 ctx->ops->runcntl_write(ctx, runcntl);
213 return 0;
216 static int spu_run_fini(struct spu_context *ctx, u32 *npc,
217 u32 *status)
219 int ret = 0;
221 *status = ctx->ops->status_read(ctx);
222 *npc = ctx->ops->npc_read(ctx);
224 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
225 spu_release(ctx);
227 if (signal_pending(current))
228 ret = -ERESTARTSYS;
230 return ret;
233 static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
234 u32 *status)
236 int ret;
238 ret = spu_run_fini(ctx, npc, status);
239 if (ret)
240 return ret;
242 if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
243 return *status;
245 ret = spu_acquire_runnable(ctx, 0);
246 if (ret)
247 return ret;
249 spuctx_switch_state(ctx, SPU_UTIL_USER);
250 return 0;
254 * SPU syscall restarting is tricky because we violate the basic
255 * assumption that the signal handler is running on the interrupted
256 * thread. Here instead, the handler runs on PowerPC user space code,
257 * while the syscall was called from the SPU.
258 * This means we can only do a very rough approximation of POSIX
259 * signal semantics.
261 static int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
262 unsigned int *npc)
264 int ret;
266 switch (*spu_ret) {
267 case -ERESTARTSYS:
268 case -ERESTARTNOINTR:
270 * Enter the regular syscall restarting for
271 * sys_spu_run, then restart the SPU syscall
272 * callback.
274 *npc -= 8;
275 ret = -ERESTARTSYS;
276 break;
277 case -ERESTARTNOHAND:
278 case -ERESTART_RESTARTBLOCK:
280 * Restart block is too hard for now, just return -EINTR
281 * to the SPU.
282 * ERESTARTNOHAND comes from sys_pause, we also return
283 * -EINTR from there.
284 * Assume that we need to be restarted ourselves though.
286 *spu_ret = -EINTR;
287 ret = -ERESTARTSYS;
288 break;
289 default:
290 printk(KERN_WARNING "%s: unexpected return code %ld\n",
291 __FUNCTION__, *spu_ret);
292 ret = 0;
294 return ret;
297 static int spu_process_callback(struct spu_context *ctx)
299 struct spu_syscall_block s;
300 u32 ls_pointer, npc;
301 void __iomem *ls;
302 long spu_ret;
303 int ret;
305 /* get syscall block from local store */
306 npc = ctx->ops->npc_read(ctx) & ~3;
307 ls = (void __iomem *)ctx->ops->get_ls(ctx);
308 ls_pointer = in_be32(ls + npc);
309 if (ls_pointer > (LS_SIZE - sizeof(s)))
310 return -EFAULT;
311 memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
313 /* do actual syscall without pinning the spu */
314 ret = 0;
315 spu_ret = -ENOSYS;
316 npc += 4;
318 if (s.nr_ret < __NR_syscalls) {
319 spu_release(ctx);
320 /* do actual system call from here */
321 spu_ret = spu_sys_callback(&s);
322 if (spu_ret <= -ERESTARTSYS) {
323 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
325 spu_acquire(ctx);
326 if (ret == -ERESTARTSYS)
327 return ret;
330 /* write result, jump over indirect pointer */
331 memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
332 ctx->ops->npc_write(ctx, npc);
333 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
334 return ret;
337 long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event)
339 int ret;
340 struct spu *spu;
341 u32 status;
343 if (mutex_lock_interruptible(&ctx->run_mutex))
344 return -ERESTARTSYS;
346 spu_enable_spu(ctx);
347 ctx->event_return = 0;
349 spu_acquire(ctx);
351 spu_update_sched_info(ctx);
353 ret = spu_run_init(ctx, npc);
354 if (ret) {
355 spu_release(ctx);
356 goto out;
359 do {
360 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
361 if (unlikely(ret))
362 break;
363 spu = ctx->spu;
364 if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE,
365 &ctx->sched_flags))) {
366 if (!(status & SPU_STATUS_STOPPED_BY_STOP)) {
367 spu_switch_notify(spu, ctx);
368 continue;
372 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
374 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
375 (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
376 ret = spu_process_callback(ctx);
377 if (ret)
378 break;
379 status &= ~SPU_STATUS_STOPPED_BY_STOP;
381 ret = spufs_handle_class1(ctx);
382 if (ret)
383 break;
385 ret = spufs_handle_class0(ctx);
386 if (ret)
387 break;
389 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
390 ret = spu_reacquire_runnable(ctx, npc, &status);
391 if (ret)
392 goto out2;
393 continue;
396 if (signal_pending(current))
397 ret = -ERESTARTSYS;
400 } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
401 SPU_STATUS_STOPPED_BY_HALT |
402 SPU_STATUS_SINGLE_STEP)));
404 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
405 (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100) &&
406 (ctx->state == SPU_STATE_RUNNABLE))
407 ctx->stats.libassist++;
410 spu_disable_spu(ctx);
411 ret = spu_run_fini(ctx, npc, &status);
412 spu_yield(ctx);
414 out2:
415 if ((ret == 0) ||
416 ((ret == -ERESTARTSYS) &&
417 ((status & SPU_STATUS_STOPPED_BY_HALT) ||
418 (status & SPU_STATUS_SINGLE_STEP) ||
419 ((status & SPU_STATUS_STOPPED_BY_STOP) &&
420 (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
421 ret = status;
423 /* Note: we don't need to force_sig SIGTRAP on single-step
424 * since we have TIF_SINGLESTEP set, thus the kernel will do
425 * it upon return from the syscall anyawy
427 if ((status & SPU_STATUS_STOPPED_BY_STOP)
428 && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
429 force_sig(SIGTRAP, current);
430 ret = -ERESTARTSYS;
433 out:
434 *event = ctx->event_return;
435 mutex_unlock(&ctx->run_mutex);
436 return ret;