blob: 3ba30cea764a8331a2f3fbe7fd98511a2c91f3a8 [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
21static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
22{
23 struct spu *spu;
24 u64 pte_fault;
25
26 *stat = ctx->ops->status_read(ctx);
27 if (ctx->state != SPU_STATE_RUNNABLE)
28 return 1;
29 spu = ctx->spu;
30 pte_fault = spu->dsisr &
31 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
Sebastian Siewiorbe703172007-06-29 10:57:50 +100032 return (!(*stat & SPU_STATUS_RUNNING) || pte_fault || spu->class_0_pending) ?
33 1 : 0;
Arnd Bergmannce8ab852006-01-04 20:31:29 +010034}
35
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010036static int spu_setup_isolated(struct spu_context *ctx)
37{
38 int ret;
39 u64 __iomem *mfc_cntl;
40 u64 sr1;
41 u32 status;
42 unsigned long timeout;
43 const u32 status_loading = SPU_STATUS_RUNNING
44 | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
45
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020046 ret = -ENODEV;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010047 if (!isolated_loader)
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010048 goto out;
49
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020050 /*
51 * We need to exclude userspace access to the context.
52 *
53 * To protect against memory access we invalidate all ptes
54 * and make sure the pagefault handlers block on the mutex.
55 */
56 spu_unmap_mappings(ctx);
57
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010058 mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
59
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",
68 __FUNCTION__);
69 ret = -EIO;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020070 goto out;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010071 }
72 cond_resched();
73 }
74
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);
79
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);
84
85 ctx->ops->runcntl_write(ctx,
86 SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
87
88 ret = 0;
89 timeout = jiffies + HZ;
90 while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
91 status_loading) {
92 if (time_after(jiffies, timeout)) {
93 printk(KERN_ERR "%s: timeout waiting for loader\n",
94 __FUNCTION__);
95 ret = -EIO;
96 goto out_drop_priv;
97 }
98 cond_resched();
99 }
100
101 if (!(status & SPU_STATUS_RUNNING)) {
102 /* If isolated LOAD has failed: run SPU, we will get a stop-and
103 * signal later. */
104 pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
105 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
106 ret = -EACCES;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200107 goto out_drop_priv;
108 }
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100109
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200110 if (!(status & SPU_STATUS_ISOLATED_STATE)) {
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100111 /* 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);
114 ret = -EINVAL;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200115 goto out_drop_priv;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100116 }
117
118out_drop_priv:
119 /* Finished accessing the loader. Drop kernel mode */
120 sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
121 spu_mfc_sr1_set(ctx->spu, sr1);
122
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100123out:
124 return ret;
125}
126
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200127static int spu_run_init(struct spu_context *ctx, u32 * npc)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100128{
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100129 if (ctx->flags & SPU_CREATE_ISOLATE) {
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200130 unsigned long runcntl;
131
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100132 if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200133 int ret = spu_setup_isolated(ctx);
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200134 if (ret)
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200135 return ret;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100136 }
137
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);
142 if (runcntl == 0)
143 runcntl = SPU_RUNCNTL_RUNNABLE;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200144 ctx->ops->runcntl_write(ctx, runcntl);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100145 } else {
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000146 unsigned long mode = SPU_PRIVCNTL_MODE_NORMAL;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100147 spu_start_tick(ctx);
Mark Nutter5737edd2006-10-24 18:31:16 +0200148 ctx->ops->npc_write(ctx, *npc);
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000149 if (test_thread_flag(TIF_SINGLESTEP))
150 mode = SPU_PRIVCNTL_MODE_SINGLE_STEP;
151 out_be64(&ctx->spu->priv2->spu_privcntl_RW, mode);
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200152 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100153 }
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100154
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200155 return 0;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100156}
157
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200158static int spu_run_fini(struct spu_context *ctx, u32 * npc,
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100159 u32 * status)
160{
161 int ret = 0;
162
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100163 spu_stop_tick(ctx);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100164 *status = ctx->ops->status_read(ctx);
165 *npc = ctx->ops->npc_read(ctx);
166 spu_release(ctx);
167
168 if (signal_pending(current))
169 ret = -ERESTARTSYS;
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100170
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100171 return ret;
172}
173
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200174static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100175 u32 *status)
176{
177 int ret;
178
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200179 ret = spu_run_fini(ctx, npc, status);
180 if (ret)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100181 return ret;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200182
183 if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100184 return *status;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200185
186 ret = spu_acquire_runnable(ctx, 0);
187 if (ret)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100188 return ret;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200189
190 ret = spu_run_init(ctx, npc);
191 if (ret) {
192 spu_release(ctx);
193 return ret;
194 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100195 return 0;
196}
197
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100198/*
199 * SPU syscall restarting is tricky because we violate the basic
200 * assumption that the signal handler is running on the interrupted
201 * thread. Here instead, the handler runs on PowerPC user space code,
202 * while the syscall was called from the SPU.
203 * This means we can only do a very rough approximation of POSIX
204 * signal semantics.
205 */
206int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
207 unsigned int *npc)
208{
209 int ret;
210
211 switch (*spu_ret) {
212 case -ERESTARTSYS:
213 case -ERESTARTNOINTR:
214 /*
215 * Enter the regular syscall restarting for
216 * sys_spu_run, then restart the SPU syscall
217 * callback.
218 */
219 *npc -= 8;
220 ret = -ERESTARTSYS;
221 break;
222 case -ERESTARTNOHAND:
223 case -ERESTART_RESTARTBLOCK:
224 /*
225 * Restart block is too hard for now, just return -EINTR
226 * to the SPU.
227 * ERESTARTNOHAND comes from sys_pause, we also return
228 * -EINTR from there.
229 * Assume that we need to be restarted ourselves though.
230 */
231 *spu_ret = -EINTR;
232 ret = -ERESTARTSYS;
233 break;
234 default:
235 printk(KERN_WARNING "%s: unexpected return code %ld\n",
236 __FUNCTION__, *spu_ret);
237 ret = 0;
238 }
239 return ret;
240}
241
242int spu_process_callback(struct spu_context *ctx)
243{
244 struct spu_syscall_block s;
245 u32 ls_pointer, npc;
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200246 void __iomem *ls;
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100247 long spu_ret;
248 int ret;
249
250 /* get syscall block from local store */
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200251 npc = ctx->ops->npc_read(ctx) & ~3;
252 ls = (void __iomem *)ctx->ops->get_ls(ctx);
253 ls_pointer = in_be32(ls + npc);
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100254 if (ls_pointer > (LS_SIZE - sizeof(s)))
255 return -EFAULT;
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200256 memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100257
258 /* do actual syscall without pinning the spu */
259 ret = 0;
260 spu_ret = -ENOSYS;
261 npc += 4;
262
263 if (s.nr_ret < __NR_syscalls) {
264 spu_release(ctx);
265 /* do actual system call from here */
266 spu_ret = spu_sys_callback(&s);
267 if (spu_ret <= -ERESTARTSYS) {
268 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
269 }
270 spu_acquire(ctx);
271 if (ret == -ERESTARTSYS)
272 return ret;
273 }
274
275 /* write result, jump over indirect pointer */
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200276 memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100277 ctx->ops->npc_write(ctx, npc);
278 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
279 return ret;
280}
281
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100282static inline int spu_process_events(struct spu_context *ctx)
283{
284 struct spu *spu = ctx->spu;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100285 int ret = 0;
286
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100287 if (spu->class_0_pending)
288 ret = spu_irq_class_0_bottom(spu);
289 if (!ret && signal_pending(current))
290 ret = -ERESTARTSYS;
291 return ret;
292}
293
294long spufs_run_spu(struct file *file, struct spu_context *ctx,
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200295 u32 *npc, u32 *event)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100296{
297 int ret;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200298 u32 status;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100299
Christoph Hellwige45d48a32007-04-23 21:08:17 +0200300 if (mutex_lock_interruptible(&ctx->run_mutex))
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100301 return -ERESTARTSYS;
302
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100303 ctx->ops->master_start(ctx);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200304 ctx->event_return = 0;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200305
306 ret = spu_acquire_runnable(ctx, 0);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100307 if (ret)
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200308 return ret;
309
310 ret = spu_run_init(ctx, npc);
311 if (ret) {
312 spu_release(ctx);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100313 goto out;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200314 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100315
316 do {
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200317 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100318 if (unlikely(ret))
319 break;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200320 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
321 (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100322 ret = spu_process_callback(ctx);
323 if (ret)
324 break;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200325 status &= ~SPU_STATUS_STOPPED_BY_STOP;
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100326 }
Arnd Bergmann57dace22007-04-23 21:08:15 +0200327 ret = spufs_handle_class1(ctx);
328 if (ret)
329 break;
330
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100331 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200332 ret = spu_reacquire_runnable(ctx, npc, &status);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100333 if (ret) {
334 spu_stop_tick(ctx);
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100335 goto out2;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100336 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100337 continue;
338 }
339 ret = spu_process_events(ctx);
340
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200341 } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000342 SPU_STATUS_STOPPED_BY_HALT |
343 SPU_STATUS_SINGLE_STEP)));
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100344
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100345 ctx->ops->master_stop(ctx);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200346 ret = spu_run_fini(ctx, npc, &status);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100347 spu_yield(ctx);
348
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100349out2:
350 if ((ret == 0) ||
351 ((ret == -ERESTARTSYS) &&
352 ((status & SPU_STATUS_STOPPED_BY_HALT) ||
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000353 (status & SPU_STATUS_SINGLE_STEP) ||
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100354 ((status & SPU_STATUS_STOPPED_BY_STOP) &&
355 (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
356 ret = status;
357
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000358 /* Note: we don't need to force_sig SIGTRAP on single-step
359 * since we have TIF_SINGLESTEP set, thus the kernel will do
360 * it upon return from the syscall anyawy
361 */
Arnd Bergmannc2b22262006-11-27 19:18:53 +0100362 if ((status & SPU_STATUS_STOPPED_BY_STOP)
363 && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
364 force_sig(SIGTRAP, current);
365 ret = -ERESTARTSYS;
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100366 }
367
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100368out:
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200369 *event = ctx->event_return;
Christoph Hellwige45d48a32007-04-23 21:08:17 +0200370 mutex_unlock(&ctx->run_mutex);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100371 return ret;
372}