3 #include <linux/wait.h>
4 #include <linux/ptrace.h>
7 #include <asm/unistd.h>
11 /* interrupt-level stop callback function. */
12 void spufs_stop_callback(struct spu *spu)
14 struct spu_context *ctx = spu->ctx;
16 wake_up_all(&ctx->stop_wq);
19 void spufs_dma_callback(struct spu *spu, int type)
21 struct spu_context *ctx = spu->ctx;
23 if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
24 ctx->event_return |= type;
25 wake_up_all(&ctx->stop_wq);
28 case SPE_EVENT_DMA_ALIGNMENT:
29 case SPE_EVENT_INVALID_DMA:
30 force_sig(SIGBUS, /* info, */ current);
32 case SPE_EVENT_SPE_ERROR:
33 force_sig(SIGILL, /* info */ current);
39 static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
44 *stat = ctx->ops->status_read(ctx);
45 if (ctx->state != SPU_STATE_RUNNABLE)
48 pte_fault = spu->dsisr &
49 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
50 return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
53 static inline int spu_run_init(struct spu_context *ctx, u32 * npc)
56 unsigned long runcntl = SPU_RUNCNTL_RUNNABLE;
58 if ((ret = spu_acquire_runnable(ctx)) != 0)
61 /* if we're in isolated mode, we would have started the SPU
62 * earlier, so don't do it again now. */
63 if (!(ctx->flags & SPU_CREATE_ISOLATE)) {
64 ctx->ops->npc_write(ctx, *npc);
65 ctx->ops->runcntl_write(ctx, runcntl);
70 static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
75 *status = ctx->ops->status_read(ctx);
76 *npc = ctx->ops->npc_read(ctx);
79 if (signal_pending(current))
81 if (unlikely(current->ptrace & PT_PTRACED)) {
82 if ((*status & SPU_STATUS_STOPPED_BY_STOP)
83 && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
84 force_sig(SIGTRAP, current);
91 static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
96 if ((ret = spu_run_fini(ctx, npc, status)) != 0)
98 if (*status & (SPU_STATUS_STOPPED_BY_STOP |
99 SPU_STATUS_STOPPED_BY_HALT)) {
102 if ((ret = spu_run_init(ctx, npc)) != 0)
108 * SPU syscall restarting is tricky because we violate the basic
109 * assumption that the signal handler is running on the interrupted
110 * thread. Here instead, the handler runs on PowerPC user space code,
111 * while the syscall was called from the SPU.
112 * This means we can only do a very rough approximation of POSIX
115 int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
122 case -ERESTARTNOINTR:
124 * Enter the regular syscall restarting for
125 * sys_spu_run, then restart the SPU syscall
131 case -ERESTARTNOHAND:
132 case -ERESTART_RESTARTBLOCK:
134 * Restart block is too hard for now, just return -EINTR
136 * ERESTARTNOHAND comes from sys_pause, we also return
138 * Assume that we need to be restarted ourselves though.
144 printk(KERN_WARNING "%s: unexpected return code %ld\n",
145 __FUNCTION__, *spu_ret);
151 int spu_process_callback(struct spu_context *ctx)
153 struct spu_syscall_block s;
159 /* get syscall block from local store */
160 npc = ctx->ops->npc_read(ctx);
161 ls = ctx->ops->get_ls(ctx);
162 ls_pointer = *(u32*)(ls + npc);
163 if (ls_pointer > (LS_SIZE - sizeof(s)))
165 memcpy(&s, ls + ls_pointer, sizeof (s));
167 /* do actual syscall without pinning the spu */
172 if (s.nr_ret < __NR_syscalls) {
174 /* do actual system call from here */
175 spu_ret = spu_sys_callback(&s);
176 if (spu_ret <= -ERESTARTSYS) {
177 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
180 if (ret == -ERESTARTSYS)
184 /* write result, jump over indirect pointer */
185 memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret));
186 ctx->ops->npc_write(ctx, npc);
187 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
191 static inline int spu_process_events(struct spu_context *ctx)
193 struct spu *spu = ctx->spu;
194 u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
197 if (spu->dsisr & pte_fault)
198 ret = spu_irq_class_1_bottom(spu);
199 if (spu->class_0_pending)
200 ret = spu_irq_class_0_bottom(spu);
201 if (!ret && signal_pending(current))
206 long spufs_run_spu(struct file *file, struct spu_context *ctx,
207 u32 *npc, u32 *event)
212 if (down_interruptible(&ctx->run_sema))
215 ctx->event_return = 0;
216 ret = spu_run_init(ctx, npc);
221 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
224 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
225 (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
226 ret = spu_process_callback(ctx);
229 status &= ~SPU_STATUS_STOPPED_BY_STOP;
231 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
232 ret = spu_reacquire_runnable(ctx, npc, &status);
237 ret = spu_process_events(ctx);
239 } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
240 SPU_STATUS_STOPPED_BY_HALT)));
242 ctx->ops->runcntl_stop(ctx);
243 ret = spu_run_fini(ctx, npc, &status);
249 *event = ctx->event_return;