blob: 0b50fa5cb39d5af406047391ad7914eafd31d9be [file] [log] [blame]
arnd@arndb.de0afacde2006-10-24 18:31:18 +02001#define DEBUG
2
Arnd Bergmannce8ab852006-01-04 20:31:29 +01003#include <linux/wait.h>
4#include <linux/ptrace.h>
5
6#include <asm/spu.h>
Jeremy Kerrc6730ed2006-11-20 18:45:10 +01007#include <asm/spu_priv1.h>
8#include <asm/io.h>
Dave Jonescfff5b22006-03-31 23:53:09 -05009#include <asm/unistd.h>
Arnd Bergmannce8ab852006-01-04 20:31:29 +010010
11#include "spufs.h"
12
13/* interrupt-level stop callback function. */
14void spufs_stop_callback(struct spu *spu)
15{
16 struct spu_context *ctx = spu->ctx;
17
18 wake_up_all(&ctx->stop_wq);
19}
20
Bob Nelson36aaccc2007-07-20 21:39:52 +020021static inline int spu_stopped(struct spu_context *ctx, u32 *stat)
Arnd Bergmannce8ab852006-01-04 20:31:29 +010022{
23 struct spu *spu;
24 u64 pte_fault;
25
26 *stat = ctx->ops->status_read(ctx);
Bob Nelson36aaccc2007-07-20 21:39:52 +020027
Arnd Bergmannce8ab852006-01-04 20:31:29 +010028 spu = ctx->spu;
Bob Nelson36aaccc2007-07-20 21:39:52 +020029 if (ctx->state != SPU_STATE_RUNNABLE ||
30 test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags))
31 return 1;
Arnd Bergmannce8ab852006-01-04 20:31:29 +010032 pte_fault = spu->dsisr &
33 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
Sebastian Siewiorbe703172007-06-29 10:57:50 +100034 return (!(*stat & SPU_STATUS_RUNNING) || pte_fault || spu->class_0_pending) ?
35 1 : 0;
Arnd Bergmannce8ab852006-01-04 20:31:29 +010036}
37
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010038static int spu_setup_isolated(struct spu_context *ctx)
39{
40 int ret;
41 u64 __iomem *mfc_cntl;
42 u64 sr1;
43 u32 status;
44 unsigned long timeout;
45 const u32 status_loading = SPU_STATUS_RUNNING
46 | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
47
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020048 ret = -ENODEV;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010049 if (!isolated_loader)
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010050 goto out;
51
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020052 /*
53 * We need to exclude userspace access to the context.
54 *
55 * To protect against memory access we invalidate all ptes
56 * and make sure the pagefault handlers block on the mutex.
57 */
58 spu_unmap_mappings(ctx);
59
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010060 mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
61
62 /* purge the MFC DMA queue to ensure no spurious accesses before we
63 * enter kernel mode */
64 timeout = jiffies + HZ;
65 out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
66 while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
67 != MFC_CNTL_PURGE_DMA_COMPLETE) {
68 if (time_after(jiffies, timeout)) {
69 printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
70 __FUNCTION__);
71 ret = -EIO;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020072 goto out;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010073 }
74 cond_resched();
75 }
76
77 /* put the SPE in kernel mode to allow access to the loader */
78 sr1 = spu_mfc_sr1_get(ctx->spu);
79 sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
80 spu_mfc_sr1_set(ctx->spu, sr1);
81
82 /* start the loader */
83 ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
84 ctx->ops->signal2_write(ctx,
85 (unsigned long)isolated_loader & 0xffffffff);
86
87 ctx->ops->runcntl_write(ctx,
88 SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
89
90 ret = 0;
91 timeout = jiffies + HZ;
92 while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
93 status_loading) {
94 if (time_after(jiffies, timeout)) {
95 printk(KERN_ERR "%s: timeout waiting for loader\n",
96 __FUNCTION__);
97 ret = -EIO;
98 goto out_drop_priv;
99 }
100 cond_resched();
101 }
102
103 if (!(status & SPU_STATUS_RUNNING)) {
104 /* If isolated LOAD has failed: run SPU, we will get a stop-and
105 * signal later. */
106 pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
107 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
108 ret = -EACCES;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200109 goto out_drop_priv;
110 }
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100111
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200112 if (!(status & SPU_STATUS_ISOLATED_STATE)) {
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100113 /* This isn't allowed by the CBEA, but check anyway */
114 pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
115 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
116 ret = -EINVAL;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200117 goto out_drop_priv;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100118 }
119
120out_drop_priv:
121 /* Finished accessing the loader. Drop kernel mode */
122 sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
123 spu_mfc_sr1_set(ctx->spu, sr1);
124
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100125out:
126 return ret;
127}
128
Bob Nelson36aaccc2007-07-20 21:39:52 +0200129static int spu_run_init(struct spu_context *ctx, u32 *npc)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100130{
Andre Detsch27ec41d2007-07-20 21:39:33 +0200131 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
132
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100133 if (ctx->flags & SPU_CREATE_ISOLATE) {
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200134 unsigned long runcntl;
135
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100136 if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200137 int ret = spu_setup_isolated(ctx);
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200138 if (ret)
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200139 return ret;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100140 }
141
142 /* if userspace has set the runcntrl register (eg, to issue an
143 * isolated exit), we need to re-set it here */
144 runcntl = ctx->ops->runcntl_read(ctx) &
145 (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
146 if (runcntl == 0)
147 runcntl = SPU_RUNCNTL_RUNNABLE;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200148 ctx->ops->runcntl_write(ctx, runcntl);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100149 } else {
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000150 unsigned long mode = SPU_PRIVCNTL_MODE_NORMAL;
Mark Nutter5737edd2006-10-24 18:31:16 +0200151 ctx->ops->npc_write(ctx, *npc);
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000152 if (test_thread_flag(TIF_SINGLESTEP))
153 mode = SPU_PRIVCNTL_MODE_SINGLE_STEP;
154 out_be64(&ctx->spu->priv2->spu_privcntl_RW, mode);
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200155 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100156 }
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100157
Andre Detsch27ec41d2007-07-20 21:39:33 +0200158 spuctx_switch_state(ctx, SPU_UTIL_USER);
159
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200160 return 0;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100161}
162
Bob Nelson36aaccc2007-07-20 21:39:52 +0200163static int spu_run_fini(struct spu_context *ctx, u32 *npc,
164 u32 *status)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100165{
166 int ret = 0;
167
168 *status = ctx->ops->status_read(ctx);
169 *npc = ctx->ops->npc_read(ctx);
Andre Detsch27ec41d2007-07-20 21:39:33 +0200170
171 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100172 spu_release(ctx);
173
174 if (signal_pending(current))
175 ret = -ERESTARTSYS;
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100176
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100177 return ret;
178}
179
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200180static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100181 u32 *status)
182{
183 int ret;
184
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200185 ret = spu_run_fini(ctx, npc, status);
186 if (ret)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100187 return ret;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200188
189 if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100190 return *status;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200191
192 ret = spu_acquire_runnable(ctx, 0);
193 if (ret)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100194 return ret;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200195
196 ret = spu_run_init(ctx, npc);
197 if (ret) {
198 spu_release(ctx);
199 return ret;
200 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100201 return 0;
202}
203
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100204/*
205 * SPU syscall restarting is tricky because we violate the basic
206 * assumption that the signal handler is running on the interrupted
207 * thread. Here instead, the handler runs on PowerPC user space code,
208 * while the syscall was called from the SPU.
209 * This means we can only do a very rough approximation of POSIX
210 * signal semantics.
211 */
212int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
213 unsigned int *npc)
214{
215 int ret;
216
217 switch (*spu_ret) {
218 case -ERESTARTSYS:
219 case -ERESTARTNOINTR:
220 /*
221 * Enter the regular syscall restarting for
222 * sys_spu_run, then restart the SPU syscall
223 * callback.
224 */
225 *npc -= 8;
226 ret = -ERESTARTSYS;
227 break;
228 case -ERESTARTNOHAND:
229 case -ERESTART_RESTARTBLOCK:
230 /*
231 * Restart block is too hard for now, just return -EINTR
232 * to the SPU.
233 * ERESTARTNOHAND comes from sys_pause, we also return
234 * -EINTR from there.
235 * Assume that we need to be restarted ourselves though.
236 */
237 *spu_ret = -EINTR;
238 ret = -ERESTARTSYS;
239 break;
240 default:
241 printk(KERN_WARNING "%s: unexpected return code %ld\n",
242 __FUNCTION__, *spu_ret);
243 ret = 0;
244 }
245 return ret;
246}
247
248int spu_process_callback(struct spu_context *ctx)
249{
250 struct spu_syscall_block s;
251 u32 ls_pointer, npc;
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200252 void __iomem *ls;
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100253 long spu_ret;
254 int ret;
255
256 /* get syscall block from local store */
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200257 npc = ctx->ops->npc_read(ctx) & ~3;
258 ls = (void __iomem *)ctx->ops->get_ls(ctx);
259 ls_pointer = in_be32(ls + npc);
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100260 if (ls_pointer > (LS_SIZE - sizeof(s)))
261 return -EFAULT;
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200262 memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100263
264 /* do actual syscall without pinning the spu */
265 ret = 0;
266 spu_ret = -ENOSYS;
267 npc += 4;
268
269 if (s.nr_ret < __NR_syscalls) {
270 spu_release(ctx);
271 /* do actual system call from here */
272 spu_ret = spu_sys_callback(&s);
273 if (spu_ret <= -ERESTARTSYS) {
274 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
275 }
276 spu_acquire(ctx);
277 if (ret == -ERESTARTSYS)
278 return ret;
279 }
280
281 /* write result, jump over indirect pointer */
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200282 memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100283 ctx->ops->npc_write(ctx, npc);
284 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
285 return ret;
286}
287
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100288static inline int spu_process_events(struct spu_context *ctx)
289{
290 struct spu *spu = ctx->spu;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100291 int ret = 0;
292
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100293 if (spu->class_0_pending)
294 ret = spu_irq_class_0_bottom(spu);
295 if (!ret && signal_pending(current))
296 ret = -ERESTARTSYS;
297 return ret;
298}
299
Jeremy Kerr50af32a2007-07-20 21:39:42 +0200300long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100301{
302 int ret;
Bob Nelson36aaccc2007-07-20 21:39:52 +0200303 struct spu *spu;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200304 u32 status;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100305
Christoph Hellwige45d48a32007-04-23 21:08:17 +0200306 if (mutex_lock_interruptible(&ctx->run_mutex))
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100307 return -ERESTARTSYS;
308
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100309 ctx->ops->master_start(ctx);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200310 ctx->event_return = 0;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200311
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000312 spu_acquire(ctx);
313 if (ctx->state == SPU_STATE_SAVED) {
314 __spu_update_sched_info(ctx);
315
316 ret = spu_activate(ctx, 0);
317 if (ret) {
318 spu_release(ctx);
319 goto out;
320 }
321 } else {
322 /*
323 * We have to update the scheduling priority under active_mutex
324 * to protect against find_victim().
325 */
326 spu_update_sched_info(ctx);
327 }
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200328
329 ret = spu_run_init(ctx, npc);
330 if (ret) {
331 spu_release(ctx);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100332 goto out;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200333 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100334
335 do {
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200336 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100337 if (unlikely(ret))
338 break;
Bob Nelson36aaccc2007-07-20 21:39:52 +0200339 spu = ctx->spu;
340 if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE,
341 &ctx->sched_flags))) {
342 if (!(status & SPU_STATUS_STOPPED_BY_STOP)) {
343 spu_switch_notify(spu, ctx);
344 continue;
345 }
346 }
Andre Detsch27ec41d2007-07-20 21:39:33 +0200347
348 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
349
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200350 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
351 (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100352 ret = spu_process_callback(ctx);
353 if (ret)
354 break;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200355 status &= ~SPU_STATUS_STOPPED_BY_STOP;
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100356 }
Arnd Bergmann57dace22007-04-23 21:08:15 +0200357 ret = spufs_handle_class1(ctx);
358 if (ret)
359 break;
360
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100361 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200362 ret = spu_reacquire_runnable(ctx, npc, &status);
Christoph Hellwig37901802007-06-29 10:57:51 +1000363 if (ret)
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100364 goto out2;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100365 continue;
366 }
367 ret = spu_process_events(ctx);
368
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200369 } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000370 SPU_STATUS_STOPPED_BY_HALT |
371 SPU_STATUS_SINGLE_STEP)));
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100372
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000373 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000374 (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100) &&
375 (ctx->state == SPU_STATE_RUNNABLE))
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000376 ctx->stats.libassist++;
377
Andre Detsch27ec41d2007-07-20 21:39:33 +0200378
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100379 ctx->ops->master_stop(ctx);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200380 ret = spu_run_fini(ctx, npc, &status);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100381 spu_yield(ctx);
382
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100383out2:
384 if ((ret == 0) ||
385 ((ret == -ERESTARTSYS) &&
386 ((status & SPU_STATUS_STOPPED_BY_HALT) ||
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000387 (status & SPU_STATUS_SINGLE_STEP) ||
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100388 ((status & SPU_STATUS_STOPPED_BY_STOP) &&
389 (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
390 ret = status;
391
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000392 /* Note: we don't need to force_sig SIGTRAP on single-step
393 * since we have TIF_SINGLESTEP set, thus the kernel will do
394 * it upon return from the syscall anyawy
395 */
Arnd Bergmannc2b22262006-11-27 19:18:53 +0100396 if ((status & SPU_STATUS_STOPPED_BY_STOP)
397 && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
398 force_sig(SIGTRAP, current);
399 ret = -ERESTARTSYS;
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100400 }
401
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100402out:
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200403 *event = ctx->event_return;
Christoph Hellwige45d48a32007-04-23 21:08:17 +0200404 mutex_unlock(&ctx->run_mutex);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100405 return ret;
406}