1 #include <linux/wait.h>
2 #include <linux/ptrace.h>
5 #include <asm/unistd.h>
9 /* interrupt-level stop callback function. */
10 void spufs_stop_callback(struct spu *spu)
12 struct spu_context *ctx = spu->ctx;
14 wake_up_all(&ctx->stop_wq);
17 static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
22 *stat = ctx->ops->status_read(ctx);
23 if (ctx->state != SPU_STATE_RUNNABLE)
26 pte_fault = spu->dsisr &
27 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
28 return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
31 static inline int spu_run_init(struct spu_context *ctx, u32 * npc,
36 if ((ret = spu_acquire_runnable(ctx)) != 0)
38 ctx->ops->npc_write(ctx, *npc);
39 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
43 static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
48 *status = ctx->ops->status_read(ctx);
49 *npc = ctx->ops->npc_read(ctx);
52 if (signal_pending(current))
55 if (unlikely(current->ptrace & PT_PTRACED)) {
56 if ((*status & SPU_STATUS_STOPPED_BY_STOP)
57 && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
58 force_sig(SIGTRAP, current);
66 static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
71 if ((ret = spu_run_fini(ctx, npc, status)) != 0)
73 if (*status & (SPU_STATUS_STOPPED_BY_STOP |
74 SPU_STATUS_STOPPED_BY_HALT)) {
77 if ((ret = spu_run_init(ctx, npc, status)) != 0)
83 * SPU syscall restarting is tricky because we violate the basic
84 * assumption that the signal handler is running on the interrupted
85 * thread. Here instead, the handler runs on PowerPC user space code,
86 * while the syscall was called from the SPU.
87 * This means we can only do a very rough approximation of POSIX
90 int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
99 * Enter the regular syscall restarting for
100 * sys_spu_run, then restart the SPU syscall
106 case -ERESTARTNOHAND:
107 case -ERESTART_RESTARTBLOCK:
109 * Restart block is too hard for now, just return -EINTR
111 * ERESTARTNOHAND comes from sys_pause, we also return
113 * Assume that we need to be restarted ourselves though.
119 printk(KERN_WARNING "%s: unexpected return code %ld\n",
120 __FUNCTION__, *spu_ret);
126 int spu_process_callback(struct spu_context *ctx)
128 struct spu_syscall_block s;
134 /* get syscall block from local store */
135 npc = ctx->ops->npc_read(ctx);
136 ls = ctx->ops->get_ls(ctx);
137 ls_pointer = *(u32*)(ls + npc);
138 if (ls_pointer > (LS_SIZE - sizeof(s)))
140 memcpy(&s, ls + ls_pointer, sizeof (s));
142 /* do actual syscall without pinning the spu */
147 if (s.nr_ret < __NR_syscalls) {
149 /* do actual system call from here */
150 spu_ret = spu_sys_callback(&s);
151 if (spu_ret <= -ERESTARTSYS) {
152 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
155 if (ret == -ERESTARTSYS)
159 /* write result, jump over indirect pointer */
160 memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret));
161 ctx->ops->npc_write(ctx, npc);
162 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
166 static inline int spu_process_events(struct spu_context *ctx)
168 struct spu *spu = ctx->spu;
169 u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
172 if (spu->dsisr & pte_fault)
173 ret = spu_irq_class_1_bottom(spu);
174 if (spu->class_0_pending)
175 ret = spu_irq_class_0_bottom(spu);
176 if (!ret && signal_pending(current))
181 long spufs_run_spu(struct file *file, struct spu_context *ctx,
182 u32 * npc, u32 * status)
186 if (down_interruptible(&ctx->run_sema))
189 ret = spu_run_init(ctx, npc, status);
194 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
197 if ((*status & SPU_STATUS_STOPPED_BY_STOP) &&
198 (*status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
199 ret = spu_process_callback(ctx);
202 *status &= ~SPU_STATUS_STOPPED_BY_STOP;
204 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
205 ret = spu_reacquire_runnable(ctx, npc, status);
210 ret = spu_process_events(ctx);
212 } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
213 SPU_STATUS_STOPPED_BY_HALT)));
215 ctx->ops->runcntl_stop(ctx);
216 ret = spu_run_fini(ctx, npc, status);