1 #include <linux/wait.h>
2 #include <linux/ptrace.h>
8 /* interrupt-level stop callback function. */
9 void spufs_stop_callback(struct spu *spu)
11 struct spu_context *ctx = spu->ctx;
13 wake_up_all(&ctx->stop_wq);
16 static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
21 *stat = ctx->ops->status_read(ctx);
22 if (ctx->state != SPU_STATE_RUNNABLE)
25 pte_fault = spu->dsisr &
26 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
27 return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
30 static inline int spu_run_init(struct spu_context *ctx, u32 * npc,
35 if ((ret = spu_acquire_runnable(ctx)) != 0)
37 ctx->ops->npc_write(ctx, *npc);
38 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
42 static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
47 *status = ctx->ops->status_read(ctx);
48 *npc = ctx->ops->npc_read(ctx);
51 if (signal_pending(current))
53 if (unlikely(current->ptrace & PT_PTRACED)) {
54 if ((*status & SPU_STATUS_STOPPED_BY_STOP)
55 && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
56 force_sig(SIGTRAP, current);
63 static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
68 if ((ret = spu_run_fini(ctx, npc, status)) != 0)
70 if (*status & (SPU_STATUS_STOPPED_BY_STOP |
71 SPU_STATUS_STOPPED_BY_HALT)) {
74 if ((ret = spu_run_init(ctx, npc, status)) != 0)
80 * SPU syscall restarting is tricky because we violate the basic
81 * assumption that the signal handler is running on the interrupted
82 * thread. Here instead, the handler runs on PowerPC user space code,
83 * while the syscall was called from the SPU.
84 * This means we can only do a very rough approximation of POSIX
87 int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
96 * Enter the regular syscall restarting for
97 * sys_spu_run, then restart the SPU syscall
103 case -ERESTARTNOHAND:
104 case -ERESTART_RESTARTBLOCK:
106 * Restart block is too hard for now, just return -EINTR
108 * ERESTARTNOHAND comes from sys_pause, we also return
110 * Assume that we need to be restarted ourselves though.
116 printk(KERN_WARNING "%s: unexpected return code %ld\n",
117 __FUNCTION__, *spu_ret);
123 int spu_process_callback(struct spu_context *ctx)
125 struct spu_syscall_block s;
131 /* get syscall block from local store */
132 npc = ctx->ops->npc_read(ctx);
133 ls = ctx->ops->get_ls(ctx);
134 ls_pointer = *(u32*)(ls + npc);
135 if (ls_pointer > (LS_SIZE - sizeof(s)))
137 memcpy(&s, ls + ls_pointer, sizeof (s));
139 /* do actual syscall without pinning the spu */
144 if (s.nr_ret < __NR_syscalls) {
146 /* do actual system call from here */
147 spu_ret = spu_sys_callback(&s);
148 if (spu_ret <= -ERESTARTSYS) {
149 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
152 if (ret == -ERESTARTSYS)
156 /* write result, jump over indirect pointer */
157 memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret));
158 ctx->ops->npc_write(ctx, npc);
159 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
163 static inline int spu_process_events(struct spu_context *ctx)
165 struct spu *spu = ctx->spu;
166 u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
169 if (spu->dsisr & pte_fault)
170 ret = spu_irq_class_1_bottom(spu);
171 if (spu->class_0_pending)
172 ret = spu_irq_class_0_bottom(spu);
173 if (!ret && signal_pending(current))
178 long spufs_run_spu(struct file *file, struct spu_context *ctx,
179 u32 * npc, u32 * status)
183 if (down_interruptible(&ctx->run_sema))
186 ret = spu_run_init(ctx, npc, status);
191 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
194 if ((*status & SPU_STATUS_STOPPED_BY_STOP) &&
195 (*status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
196 ret = spu_process_callback(ctx);
199 *status &= ~SPU_STATUS_STOPPED_BY_STOP;
201 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
202 ret = spu_reacquire_runnable(ctx, npc, status);
207 ret = spu_process_events(ctx);
209 } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
210 SPU_STATUS_STOPPED_BY_HALT)));
212 ctx->ops->runcntl_stop(ctx);
213 ret = spu_run_fini(ctx, npc, status);