3 #include <linux/wait.h>
4 #include <linux/ptrace.h>
7 #include <asm/spu_priv1.h>
9 #include <asm/unistd.h>
13 /* interrupt-level stop callback function. */
14 void spufs_stop_callback(struct spu
*spu
)
16 struct spu_context
*ctx
= spu
->ctx
;
18 wake_up_all(&ctx
->stop_wq
);
21 static inline int spu_stopped(struct spu_context
*ctx
, u32
* stat
)
26 *stat
= ctx
->ops
->status_read(ctx
);
27 if (ctx
->state
!= SPU_STATE_RUNNABLE
)
30 pte_fault
= spu
->dsisr
&
31 (MFC_DSISR_PTE_NOT_FOUND
| MFC_DSISR_ACCESS_DENIED
);
32 return (!(*stat
& SPU_STATUS_RUNNING
) || pte_fault
|| spu
->class_0_pending
) ?
36 static int spu_setup_isolated(struct spu_context
*ctx
)
39 u64 __iomem
*mfc_cntl
;
42 unsigned long timeout
;
43 const u32 status_loading
= SPU_STATUS_RUNNING
44 | SPU_STATUS_ISOLATED_STATE
| SPU_STATUS_ISOLATED_LOAD_STATUS
;
51 * We need to exclude userspace access to the context.
53 * To protect against memory access we invalidate all ptes
54 * and make sure the pagefault handlers block on the mutex.
56 spu_unmap_mappings(ctx
);
58 mfc_cntl
= &ctx
->spu
->priv2
->mfc_control_RW
;
60 /* purge the MFC DMA queue to ensure no spurious accesses before we
61 * enter kernel mode */
62 timeout
= jiffies
+ HZ
;
63 out_be64(mfc_cntl
, MFC_CNTL_PURGE_DMA_REQUEST
);
64 while ((in_be64(mfc_cntl
) & MFC_CNTL_PURGE_DMA_STATUS_MASK
)
65 != MFC_CNTL_PURGE_DMA_COMPLETE
) {
66 if (time_after(jiffies
, timeout
)) {
67 printk(KERN_ERR
"%s: timeout flushing MFC DMA queue\n",
75 /* put the SPE in kernel mode to allow access to the loader */
76 sr1
= spu_mfc_sr1_get(ctx
->spu
);
77 sr1
&= ~MFC_STATE1_PROBLEM_STATE_MASK
;
78 spu_mfc_sr1_set(ctx
->spu
, sr1
);
80 /* start the loader */
81 ctx
->ops
->signal1_write(ctx
, (unsigned long)isolated_loader
>> 32);
82 ctx
->ops
->signal2_write(ctx
,
83 (unsigned long)isolated_loader
& 0xffffffff);
85 ctx
->ops
->runcntl_write(ctx
,
86 SPU_RUNCNTL_RUNNABLE
| SPU_RUNCNTL_ISOLATE
);
89 timeout
= jiffies
+ HZ
;
90 while (((status
= ctx
->ops
->status_read(ctx
)) & status_loading
) ==
92 if (time_after(jiffies
, timeout
)) {
93 printk(KERN_ERR
"%s: timeout waiting for loader\n",
101 if (!(status
& SPU_STATUS_RUNNING
)) {
102 /* If isolated LOAD has failed: run SPU, we will get a stop-and
104 pr_debug("%s: isolated LOAD failed\n", __FUNCTION__
);
105 ctx
->ops
->runcntl_write(ctx
, SPU_RUNCNTL_RUNNABLE
);
110 if (!(status
& SPU_STATUS_ISOLATED_STATE
)) {
111 /* This isn't allowed by the CBEA, but check anyway */
112 pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__
);
113 ctx
->ops
->runcntl_write(ctx
, SPU_RUNCNTL_STOP
);
119 /* Finished accessing the loader. Drop kernel mode */
120 sr1
|= MFC_STATE1_PROBLEM_STATE_MASK
;
121 spu_mfc_sr1_set(ctx
->spu
, sr1
);
127 static int spu_run_init(struct spu_context
*ctx
, u32
* npc
)
129 if (ctx
->flags
& SPU_CREATE_ISOLATE
) {
130 unsigned long runcntl
;
132 if (!(ctx
->ops
->status_read(ctx
) & SPU_STATUS_ISOLATED_STATE
)) {
133 int ret
= spu_setup_isolated(ctx
);
138 /* if userspace has set the runcntrl register (eg, to issue an
139 * isolated exit), we need to re-set it here */
140 runcntl
= ctx
->ops
->runcntl_read(ctx
) &
141 (SPU_RUNCNTL_RUNNABLE
| SPU_RUNCNTL_ISOLATE
);
143 runcntl
= SPU_RUNCNTL_RUNNABLE
;
144 ctx
->ops
->runcntl_write(ctx
, runcntl
);
146 unsigned long mode
= SPU_PRIVCNTL_MODE_NORMAL
;
147 ctx
->ops
->npc_write(ctx
, *npc
);
148 if (test_thread_flag(TIF_SINGLESTEP
))
149 mode
= SPU_PRIVCNTL_MODE_SINGLE_STEP
;
150 out_be64(&ctx
->spu
->priv2
->spu_privcntl_RW
, mode
);
151 ctx
->ops
->runcntl_write(ctx
, SPU_RUNCNTL_RUNNABLE
);
157 static int spu_run_fini(struct spu_context
*ctx
, u32
* npc
,
162 *status
= ctx
->ops
->status_read(ctx
);
163 *npc
= ctx
->ops
->npc_read(ctx
);
166 if (signal_pending(current
))
172 static int spu_reacquire_runnable(struct spu_context
*ctx
, u32
*npc
,
177 ret
= spu_run_fini(ctx
, npc
, status
);
181 if (*status
& (SPU_STATUS_STOPPED_BY_STOP
| SPU_STATUS_STOPPED_BY_HALT
))
184 ret
= spu_acquire_runnable(ctx
, 0);
188 ret
= spu_run_init(ctx
, npc
);
197 * SPU syscall restarting is tricky because we violate the basic
198 * assumption that the signal handler is running on the interrupted
199 * thread. Here instead, the handler runs on PowerPC user space code,
200 * while the syscall was called from the SPU.
201 * This means we can only do a very rough approximation of POSIX
204 int spu_handle_restartsys(struct spu_context
*ctx
, long *spu_ret
,
211 case -ERESTARTNOINTR
:
213 * Enter the regular syscall restarting for
214 * sys_spu_run, then restart the SPU syscall
220 case -ERESTARTNOHAND
:
221 case -ERESTART_RESTARTBLOCK
:
223 * Restart block is too hard for now, just return -EINTR
225 * ERESTARTNOHAND comes from sys_pause, we also return
227 * Assume that we need to be restarted ourselves though.
233 printk(KERN_WARNING
"%s: unexpected return code %ld\n",
234 __FUNCTION__
, *spu_ret
);
240 int spu_process_callback(struct spu_context
*ctx
)
242 struct spu_syscall_block s
;
248 /* get syscall block from local store */
249 npc
= ctx
->ops
->npc_read(ctx
) & ~3;
250 ls
= (void __iomem
*)ctx
->ops
->get_ls(ctx
);
251 ls_pointer
= in_be32(ls
+ npc
);
252 if (ls_pointer
> (LS_SIZE
- sizeof(s
)))
254 memcpy_fromio(&s
, ls
+ ls_pointer
, sizeof(s
));
256 /* do actual syscall without pinning the spu */
261 if (s
.nr_ret
< __NR_syscalls
) {
263 /* do actual system call from here */
264 spu_ret
= spu_sys_callback(&s
);
265 if (spu_ret
<= -ERESTARTSYS
) {
266 ret
= spu_handle_restartsys(ctx
, &spu_ret
, &npc
);
269 if (ret
== -ERESTARTSYS
)
273 /* write result, jump over indirect pointer */
274 memcpy_toio(ls
+ ls_pointer
, &spu_ret
, sizeof(spu_ret
));
275 ctx
->ops
->npc_write(ctx
, npc
);
276 ctx
->ops
->runcntl_write(ctx
, SPU_RUNCNTL_RUNNABLE
);
280 static inline int spu_process_events(struct spu_context
*ctx
)
282 struct spu
*spu
= ctx
->spu
;
285 if (spu
->class_0_pending
)
286 ret
= spu_irq_class_0_bottom(spu
);
287 if (!ret
&& signal_pending(current
))
292 long spufs_run_spu(struct file
*file
, struct spu_context
*ctx
,
293 u32
*npc
, u32
*event
)
298 if (mutex_lock_interruptible(&ctx
->run_mutex
))
301 ctx
->ops
->master_start(ctx
);
302 ctx
->event_return
= 0;
305 if (ctx
->state
== SPU_STATE_SAVED
) {
306 __spu_update_sched_info(ctx
);
308 ret
= spu_activate(ctx
, 0);
315 * We have to update the scheduling priority under active_mutex
316 * to protect against find_victim().
318 spu_update_sched_info(ctx
);
321 ret
= spu_run_init(ctx
, npc
);
328 ret
= spufs_wait(ctx
->stop_wq
, spu_stopped(ctx
, &status
));
331 if ((status
& SPU_STATUS_STOPPED_BY_STOP
) &&
332 (status
>> SPU_STOP_STATUS_SHIFT
== 0x2104)) {
333 ret
= spu_process_callback(ctx
);
336 status
&= ~SPU_STATUS_STOPPED_BY_STOP
;
338 ret
= spufs_handle_class1(ctx
);
342 if (unlikely(ctx
->state
!= SPU_STATE_RUNNABLE
)) {
343 ret
= spu_reacquire_runnable(ctx
, npc
, &status
);
348 ret
= spu_process_events(ctx
);
350 } while (!ret
&& !(status
& (SPU_STATUS_STOPPED_BY_STOP
|
351 SPU_STATUS_STOPPED_BY_HALT
|
352 SPU_STATUS_SINGLE_STEP
)));
354 ctx
->ops
->master_stop(ctx
);
355 ret
= spu_run_fini(ctx
, npc
, &status
);
360 ((ret
== -ERESTARTSYS
) &&
361 ((status
& SPU_STATUS_STOPPED_BY_HALT
) ||
362 (status
& SPU_STATUS_SINGLE_STEP
) ||
363 ((status
& SPU_STATUS_STOPPED_BY_STOP
) &&
364 (status
>> SPU_STOP_STATUS_SHIFT
!= 0x2104)))))
367 /* Note: we don't need to force_sig SIGTRAP on single-step
368 * since we have TIF_SINGLESTEP set, thus the kernel will do
369 * it upon return from the syscall anyawy
371 if ((status
& SPU_STATUS_STOPPED_BY_STOP
)
372 && (status
>> SPU_STOP_STATUS_SHIFT
) == 0x3fff) {
373 force_sig(SIGTRAP
, current
);
378 *event
= ctx
->event_return
;
379 mutex_unlock(&ctx
->run_mutex
);