blob: 6625ed2a7fddcbb3808deb3f4b163e3ca37326cb [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);
32 return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
33}
34
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010035static int spu_setup_isolated(struct spu_context *ctx)
36{
37 int ret;
38 u64 __iomem *mfc_cntl;
39 u64 sr1;
40 u32 status;
41 unsigned long timeout;
42 const u32 status_loading = SPU_STATUS_RUNNING
43 | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
44
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020045 ret = -ENODEV;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010046 if (!isolated_loader)
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010047 goto out;
48
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020049 /*
50 * We need to exclude userspace access to the context.
51 *
52 * To protect against memory access we invalidate all ptes
53 * and make sure the pagefault handlers block on the mutex.
54 */
55 spu_unmap_mappings(ctx);
56
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010057 mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
58
59 /* purge the MFC DMA queue to ensure no spurious accesses before we
60 * enter kernel mode */
61 timeout = jiffies + HZ;
62 out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
63 while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
64 != MFC_CNTL_PURGE_DMA_COMPLETE) {
65 if (time_after(jiffies, timeout)) {
66 printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
67 __FUNCTION__);
68 ret = -EIO;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +020069 goto out;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010070 }
71 cond_resched();
72 }
73
74 /* put the SPE in kernel mode to allow access to the loader */
75 sr1 = spu_mfc_sr1_get(ctx->spu);
76 sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
77 spu_mfc_sr1_set(ctx->spu, sr1);
78
79 /* start the loader */
80 ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
81 ctx->ops->signal2_write(ctx,
82 (unsigned long)isolated_loader & 0xffffffff);
83
84 ctx->ops->runcntl_write(ctx,
85 SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
86
87 ret = 0;
88 timeout = jiffies + HZ;
89 while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
90 status_loading) {
91 if (time_after(jiffies, timeout)) {
92 printk(KERN_ERR "%s: timeout waiting for loader\n",
93 __FUNCTION__);
94 ret = -EIO;
95 goto out_drop_priv;
96 }
97 cond_resched();
98 }
99
100 if (!(status & SPU_STATUS_RUNNING)) {
101 /* If isolated LOAD has failed: run SPU, we will get a stop-and
102 * signal later. */
103 pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
104 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
105 ret = -EACCES;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200106 goto out_drop_priv;
107 }
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100108
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200109 if (!(status & SPU_STATUS_ISOLATED_STATE)) {
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100110 /* This isn't allowed by the CBEA, but check anyway */
111 pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
112 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
113 ret = -EINVAL;
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200114 goto out_drop_priv;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100115 }
116
117out_drop_priv:
118 /* Finished accessing the loader. Drop kernel mode */
119 sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
120 spu_mfc_sr1_set(ctx->spu, sr1);
121
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100122out:
123 return ret;
124}
125
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200126static int spu_run_init(struct spu_context *ctx, u32 * npc)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100127{
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100128 if (ctx->flags & SPU_CREATE_ISOLATE) {
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200129 unsigned long runcntl;
130
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100131 if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200132 int ret = spu_setup_isolated(ctx);
Christoph Hellwig7ec18ab2007-04-23 21:08:12 +0200133 if (ret)
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200134 return ret;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100135 }
136
137 /* if userspace has set the runcntrl register (eg, to issue an
138 * isolated exit), we need to re-set it here */
139 runcntl = ctx->ops->runcntl_read(ctx) &
140 (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
141 if (runcntl == 0)
142 runcntl = SPU_RUNCNTL_RUNNABLE;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200143 ctx->ops->runcntl_write(ctx, runcntl);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100144 } else {
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000145 unsigned long mode = SPU_PRIVCNTL_MODE_NORMAL;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100146 spu_start_tick(ctx);
Mark Nutter5737edd2006-10-24 18:31:16 +0200147 ctx->ops->npc_write(ctx, *npc);
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000148 if (test_thread_flag(TIF_SINGLESTEP))
149 mode = SPU_PRIVCNTL_MODE_SINGLE_STEP;
150 out_be64(&ctx->spu->priv2->spu_privcntl_RW, mode);
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200151 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100152 }
Jeremy Kerrc6730ed2006-11-20 18:45:10 +0100153
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200154 return 0;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100155}
156
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200157static int spu_run_fini(struct spu_context *ctx, u32 * npc,
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100158 u32 * status)
159{
160 int ret = 0;
161
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100162 spu_stop_tick(ctx);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100163 *status = ctx->ops->status_read(ctx);
164 *npc = ctx->ops->npc_read(ctx);
165 spu_release(ctx);
166
167 if (signal_pending(current))
168 ret = -ERESTARTSYS;
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100169
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100170 return ret;
171}
172
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200173static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100174 u32 *status)
175{
176 int ret;
177
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200178 ret = spu_run_fini(ctx, npc, status);
179 if (ret)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100180 return ret;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200181
182 if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100183 return *status;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200184
185 ret = spu_acquire_runnable(ctx, 0);
186 if (ret)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100187 return ret;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200188
189 ret = spu_run_init(ctx, npc);
190 if (ret) {
191 spu_release(ctx);
192 return ret;
193 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100194 return 0;
195}
196
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100197/*
198 * SPU syscall restarting is tricky because we violate the basic
199 * assumption that the signal handler is running on the interrupted
200 * thread. Here instead, the handler runs on PowerPC user space code,
201 * while the syscall was called from the SPU.
202 * This means we can only do a very rough approximation of POSIX
203 * signal semantics.
204 */
205int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
206 unsigned int *npc)
207{
208 int ret;
209
210 switch (*spu_ret) {
211 case -ERESTARTSYS:
212 case -ERESTARTNOINTR:
213 /*
214 * Enter the regular syscall restarting for
215 * sys_spu_run, then restart the SPU syscall
216 * callback.
217 */
218 *npc -= 8;
219 ret = -ERESTARTSYS;
220 break;
221 case -ERESTARTNOHAND:
222 case -ERESTART_RESTARTBLOCK:
223 /*
224 * Restart block is too hard for now, just return -EINTR
225 * to the SPU.
226 * ERESTARTNOHAND comes from sys_pause, we also return
227 * -EINTR from there.
228 * Assume that we need to be restarted ourselves though.
229 */
230 *spu_ret = -EINTR;
231 ret = -ERESTARTSYS;
232 break;
233 default:
234 printk(KERN_WARNING "%s: unexpected return code %ld\n",
235 __FUNCTION__, *spu_ret);
236 ret = 0;
237 }
238 return ret;
239}
240
241int spu_process_callback(struct spu_context *ctx)
242{
243 struct spu_syscall_block s;
244 u32 ls_pointer, npc;
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200245 void __iomem *ls;
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100246 long spu_ret;
247 int ret;
248
249 /* get syscall block from local store */
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200250 npc = ctx->ops->npc_read(ctx) & ~3;
251 ls = (void __iomem *)ctx->ops->get_ls(ctx);
252 ls_pointer = in_be32(ls + npc);
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100253 if (ls_pointer > (LS_SIZE - sizeof(s)))
254 return -EFAULT;
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200255 memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100256
257 /* do actual syscall without pinning the spu */
258 ret = 0;
259 spu_ret = -ENOSYS;
260 npc += 4;
261
262 if (s.nr_ret < __NR_syscalls) {
263 spu_release(ctx);
264 /* do actual system call from here */
265 spu_ret = spu_sys_callback(&s);
266 if (spu_ret <= -ERESTARTSYS) {
267 ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
268 }
269 spu_acquire(ctx);
270 if (ret == -ERESTARTSYS)
271 return ret;
272 }
273
274 /* write result, jump over indirect pointer */
Akinobu Mita9e2fe2c2007-04-23 21:08:22 +0200275 memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100276 ctx->ops->npc_write(ctx, npc);
277 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
278 return ret;
279}
280
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100281static inline int spu_process_events(struct spu_context *ctx)
282{
283 struct spu *spu = ctx->spu;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100284 int ret = 0;
285
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100286 if (spu->class_0_pending)
287 ret = spu_irq_class_0_bottom(spu);
288 if (!ret && signal_pending(current))
289 ret = -ERESTARTSYS;
290 return ret;
291}
292
293long spufs_run_spu(struct file *file, struct spu_context *ctx,
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200294 u32 *npc, u32 *event)
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100295{
296 int ret;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200297 u32 status;
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100298
Christoph Hellwige45d48a32007-04-23 21:08:17 +0200299 if (mutex_lock_interruptible(&ctx->run_mutex))
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100300 return -ERESTARTSYS;
301
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100302 ctx->ops->master_start(ctx);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200303 ctx->event_return = 0;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200304
305 ret = spu_acquire_runnable(ctx, 0);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100306 if (ret)
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200307 return ret;
308
309 ret = spu_run_init(ctx, npc);
310 if (ret) {
311 spu_release(ctx);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100312 goto out;
Christoph Hellwigaa45e252007-04-23 21:08:27 +0200313 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100314
315 do {
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200316 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100317 if (unlikely(ret))
318 break;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200319 if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
320 (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100321 ret = spu_process_callback(ctx);
322 if (ret)
323 break;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200324 status &= ~SPU_STATUS_STOPPED_BY_STOP;
Arnd Bergmann2dd14932006-03-23 00:00:09 +0100325 }
Arnd Bergmann57dace22007-04-23 21:08:15 +0200326 ret = spufs_handle_class1(ctx);
327 if (ret)
328 break;
329
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100330 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200331 ret = spu_reacquire_runnable(ctx, npc, &status);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100332 if (ret) {
333 spu_stop_tick(ctx);
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100334 goto out2;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100335 }
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100336 continue;
337 }
338 ret = spu_process_events(ctx);
339
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200340 } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000341 SPU_STATUS_STOPPED_BY_HALT |
342 SPU_STATUS_SINGLE_STEP)));
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100343
Arnd Bergmannee2d7342006-11-20 18:45:08 +0100344 ctx->ops->master_stop(ctx);
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200345 ret = spu_run_fini(ctx, npc, &status);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100346 spu_yield(ctx);
347
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100348out2:
349 if ((ret == 0) ||
350 ((ret == -ERESTARTSYS) &&
351 ((status & SPU_STATUS_STOPPED_BY_HALT) ||
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000352 (status & SPU_STATUS_SINGLE_STEP) ||
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100353 ((status & SPU_STATUS_STOPPED_BY_STOP) &&
354 (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
355 ret = status;
356
Benjamin Herrenschmidt05169232007-06-04 15:15:37 +1000357 /* Note: we don't need to force_sig SIGTRAP on single-step
358 * since we have TIF_SINGLESTEP set, thus the kernel will do
359 * it upon return from the syscall anyawy
360 */
Arnd Bergmannc2b22262006-11-27 19:18:53 +0100361 if ((status & SPU_STATUS_STOPPED_BY_STOP)
362 && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
363 force_sig(SIGTRAP, current);
364 ret = -ERESTARTSYS;
Masato Noguchi2ebb2472006-11-20 18:45:04 +0100365 }
366
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100367out:
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200368 *event = ctx->event_return;
Christoph Hellwige45d48a32007-04-23 21:08:17 +0200369 mutex_unlock(&ctx->run_mutex);
Arnd Bergmannce8ab852006-01-04 20:31:29 +0100370 return ret;
371}