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))
54 if (unlikely(current->ptrace & PT_PTRACED)) {
55 if ((*status & SPU_STATUS_STOPPED_BY_STOP)
56 && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
57 force_sig(SIGTRAP, current);
64 static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
69 if ((ret = spu_run_fini(ctx, npc, status)) != 0)
71 if (*status & (SPU_STATUS_STOPPED_BY_STOP |
72 SPU_STATUS_STOPPED_BY_HALT)) {
75 if ((ret = spu_run_init(ctx, npc, status)) != 0)
81 * SPU syscall restarting is tricky because we violate the basic
82 * assumption that the signal handler is running on the interrupted
83 * thread. Here instead, the handler runs on PowerPC user space code,
84 * while the syscall was called from the SPU.
85 * This means we can only do a very rough approximation of POSIX
88 int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
97 * Enter the regular syscall restarting for
98 * sys_spu_run, then restart the SPU syscall
104 case -ERESTARTNOHAND:
105 case -ERESTART_RESTARTBLOCK:
107 * Restart block is too hard for now, just return -EINTR
109 * ERESTARTNOHAND comes from sys_pause, we also return
111 * Assume that we need to be restarted ourselves though.
117 printk(KERN_WARNING "%s: unexpected return code %ld\n",
118 __FUNCTION__, *spu_ret);
124 int spu_process_callback(struct spu_context *ctx)
126 struct spu_syscall_block s;
132 /* get syscall block from local store */
133 npc = ctx->ops->npc_read(ctx);
134 ls = ctx->ops->get_ls(ctx);
135 ls_pointer = *(u32*)(ls + npc);
136 if (ls_pointer > (LS_SIZE - sizeof(s)))
138 memcpy(&s, ls + ls_pointer, sizeof (s));
140 /* do actual syscall without pinning the spu */
145 if (s.nr_ret < __NR_syscalls) {
147 /* do actual system call from here */
148 spu_ret = spu_sys_callback(&s);
149 if (spu_ret <= -ERESTARTSYS) {
150 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
153 if (ret == -ERESTARTSYS)
157 /* write result, jump over indirect pointer */
158 memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret));
159 ctx->ops->npc_write(ctx, npc);
160 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
164 static inline int spu_process_events(struct spu_context *ctx)
166 struct spu *spu = ctx->spu;
167 u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
170 if (spu->dsisr & pte_fault)
171 ret = spu_irq_class_1_bottom(spu);
172 if (spu->class_0_pending)
173 ret = spu_irq_class_0_bottom(spu);
174 if (!ret && signal_pending(current))
179 long spufs_run_spu(struct file *file, struct spu_context *ctx,
180 u32 * npc, u32 * status)
184 if (down_interruptible(&ctx->run_sema))
187 ret = spu_run_init(ctx, npc, status);
192 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
195 if ((*status & SPU_STATUS_STOPPED_BY_STOP) &&
196 (*status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
197 ret = spu_process_callback(ctx);
200 *status &= ~SPU_STATUS_STOPPED_BY_STOP;
202 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
203 ret = spu_reacquire_runnable(ctx, npc, status);
208 ret = spu_process_events(ctx);
210 } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
211 SPU_STATUS_STOPPED_BY_HALT)));
213 ctx->ops->runcntl_stop(ctx);
214 ret = spu_run_fini(ctx, npc, status);