10afacde3Sarnd@arndb.de #define DEBUG
20afacde3Sarnd@arndb.de 
3ce8ab854SArnd Bergmann #include <linux/wait.h>
4ce8ab854SArnd Bergmann #include <linux/ptrace.h>
5ce8ab854SArnd Bergmann 
6ce8ab854SArnd Bergmann #include <asm/spu.h>
7c6730ed4SJeremy Kerr #include <asm/spu_priv1.h>
8c6730ed4SJeremy Kerr #include <asm/io.h>
9cfff5b23SDave Jones #include <asm/unistd.h>
10ce8ab854SArnd Bergmann 
11ce8ab854SArnd Bergmann #include "spufs.h"
12ce8ab854SArnd Bergmann 
13ce8ab854SArnd Bergmann /* interrupt-level stop callback function. */
14ce8ab854SArnd Bergmann void spufs_stop_callback(struct spu *spu)
15ce8ab854SArnd Bergmann {
16ce8ab854SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
17ce8ab854SArnd Bergmann 
18ce8ab854SArnd Bergmann 	wake_up_all(&ctx->stop_wq);
19ce8ab854SArnd Bergmann }
20ce8ab854SArnd Bergmann 
21ce8ab854SArnd Bergmann static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
22ce8ab854SArnd Bergmann {
23ce8ab854SArnd Bergmann 	struct spu *spu;
24ce8ab854SArnd Bergmann 	u64 pte_fault;
25ce8ab854SArnd Bergmann 
26ce8ab854SArnd Bergmann 	*stat = ctx->ops->status_read(ctx);
27ce8ab854SArnd Bergmann 	if (ctx->state != SPU_STATE_RUNNABLE)
28ce8ab854SArnd Bergmann 		return 1;
29ce8ab854SArnd Bergmann 	spu = ctx->spu;
30ce8ab854SArnd Bergmann 	pte_fault = spu->dsisr &
31ce8ab854SArnd Bergmann 	    (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
32be703177SSebastian Siewior 	return (!(*stat & SPU_STATUS_RUNNING) || pte_fault || spu->class_0_pending) ?
33be703177SSebastian Siewior 		1 : 0;
34ce8ab854SArnd Bergmann }
35ce8ab854SArnd Bergmann 
36c6730ed4SJeremy Kerr static int spu_setup_isolated(struct spu_context *ctx)
37c6730ed4SJeremy Kerr {
38c6730ed4SJeremy Kerr 	int ret;
39c6730ed4SJeremy Kerr 	u64 __iomem *mfc_cntl;
40c6730ed4SJeremy Kerr 	u64 sr1;
41c6730ed4SJeremy Kerr 	u32 status;
42c6730ed4SJeremy Kerr 	unsigned long timeout;
43c6730ed4SJeremy Kerr 	const u32 status_loading = SPU_STATUS_RUNNING
44c6730ed4SJeremy Kerr 		| SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
45c6730ed4SJeremy Kerr 
467ec18ab9SChristoph Hellwig 	ret = -ENODEV;
47c6730ed4SJeremy Kerr 	if (!isolated_loader)
48c6730ed4SJeremy Kerr 		goto out;
49c6730ed4SJeremy Kerr 
507ec18ab9SChristoph Hellwig 	/*
517ec18ab9SChristoph Hellwig 	 * We need to exclude userspace access to the context.
527ec18ab9SChristoph Hellwig 	 *
537ec18ab9SChristoph Hellwig 	 * To protect against memory access we invalidate all ptes
547ec18ab9SChristoph Hellwig 	 * and make sure the pagefault handlers block on the mutex.
557ec18ab9SChristoph Hellwig 	 */
567ec18ab9SChristoph Hellwig 	spu_unmap_mappings(ctx);
577ec18ab9SChristoph Hellwig 
58c6730ed4SJeremy Kerr 	mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
59c6730ed4SJeremy Kerr 
60c6730ed4SJeremy Kerr 	/* purge the MFC DMA queue to ensure no spurious accesses before we
61c6730ed4SJeremy Kerr 	 * enter kernel mode */
62c6730ed4SJeremy Kerr 	timeout = jiffies + HZ;
63c6730ed4SJeremy Kerr 	out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
64c6730ed4SJeremy Kerr 	while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
65c6730ed4SJeremy Kerr 			!= MFC_CNTL_PURGE_DMA_COMPLETE) {
66c6730ed4SJeremy Kerr 		if (time_after(jiffies, timeout)) {
67c6730ed4SJeremy Kerr 			printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
68c6730ed4SJeremy Kerr 					__FUNCTION__);
69c6730ed4SJeremy Kerr 			ret = -EIO;
707ec18ab9SChristoph Hellwig 			goto out;
71c6730ed4SJeremy Kerr 		}
72c6730ed4SJeremy Kerr 		cond_resched();
73c6730ed4SJeremy Kerr 	}
74c6730ed4SJeremy Kerr 
75c6730ed4SJeremy Kerr 	/* put the SPE in kernel mode to allow access to the loader */
76c6730ed4SJeremy Kerr 	sr1 = spu_mfc_sr1_get(ctx->spu);
77c6730ed4SJeremy Kerr 	sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
78c6730ed4SJeremy Kerr 	spu_mfc_sr1_set(ctx->spu, sr1);
79c6730ed4SJeremy Kerr 
80c6730ed4SJeremy Kerr 	/* start the loader */
81c6730ed4SJeremy Kerr 	ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
82c6730ed4SJeremy Kerr 	ctx->ops->signal2_write(ctx,
83c6730ed4SJeremy Kerr 			(unsigned long)isolated_loader & 0xffffffff);
84c6730ed4SJeremy Kerr 
85c6730ed4SJeremy Kerr 	ctx->ops->runcntl_write(ctx,
86c6730ed4SJeremy Kerr 			SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
87c6730ed4SJeremy Kerr 
88c6730ed4SJeremy Kerr 	ret = 0;
89c6730ed4SJeremy Kerr 	timeout = jiffies + HZ;
90c6730ed4SJeremy Kerr 	while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
91c6730ed4SJeremy Kerr 				status_loading) {
92c6730ed4SJeremy Kerr 		if (time_after(jiffies, timeout)) {
93c6730ed4SJeremy Kerr 			printk(KERN_ERR "%s: timeout waiting for loader\n",
94c6730ed4SJeremy Kerr 					__FUNCTION__);
95c6730ed4SJeremy Kerr 			ret = -EIO;
96c6730ed4SJeremy Kerr 			goto out_drop_priv;
97c6730ed4SJeremy Kerr 		}
98c6730ed4SJeremy Kerr 		cond_resched();
99c6730ed4SJeremy Kerr 	}
100c6730ed4SJeremy Kerr 
101c6730ed4SJeremy Kerr 	if (!(status & SPU_STATUS_RUNNING)) {
102c6730ed4SJeremy Kerr 		/* If isolated LOAD has failed: run SPU, we will get a stop-and
103c6730ed4SJeremy Kerr 		 * signal later. */
104c6730ed4SJeremy Kerr 		pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
105c6730ed4SJeremy Kerr 		ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
106c6730ed4SJeremy Kerr 		ret = -EACCES;
1077ec18ab9SChristoph Hellwig 		goto out_drop_priv;
1087ec18ab9SChristoph Hellwig 	}
109c6730ed4SJeremy Kerr 
1107ec18ab9SChristoph Hellwig 	if (!(status & SPU_STATUS_ISOLATED_STATE)) {
111c6730ed4SJeremy Kerr 		/* This isn't allowed by the CBEA, but check anyway */
112c6730ed4SJeremy Kerr 		pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
113c6730ed4SJeremy Kerr 		ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
114c6730ed4SJeremy Kerr 		ret = -EINVAL;
1157ec18ab9SChristoph Hellwig 		goto out_drop_priv;
116c6730ed4SJeremy Kerr 	}
117c6730ed4SJeremy Kerr 
118c6730ed4SJeremy Kerr out_drop_priv:
119c6730ed4SJeremy Kerr 	/* Finished accessing the loader. Drop kernel mode */
120c6730ed4SJeremy Kerr 	sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
121c6730ed4SJeremy Kerr 	spu_mfc_sr1_set(ctx->spu, sr1);
122c6730ed4SJeremy Kerr 
123c6730ed4SJeremy Kerr out:
124c6730ed4SJeremy Kerr 	return ret;
125c6730ed4SJeremy Kerr }
126c6730ed4SJeremy Kerr 
127aa45e256SChristoph Hellwig static int spu_run_init(struct spu_context *ctx, u32 * npc)
128ce8ab854SArnd Bergmann {
129aa45e256SChristoph Hellwig 	if (ctx->flags & SPU_CREATE_ISOLATE) {
130aa45e256SChristoph Hellwig 		unsigned long runcntl;
131ce8ab854SArnd Bergmann 
132aa45e256SChristoph Hellwig 		if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
133aa45e256SChristoph Hellwig 			int ret = spu_setup_isolated(ctx);
134c6730ed4SJeremy Kerr 			if (ret)
135ce8ab854SArnd Bergmann 				return ret;
1360afacde3Sarnd@arndb.de 		}
137c6730ed4SJeremy Kerr 
138c6730ed4SJeremy Kerr 		/* if userspace has set the runcntrl register (eg, to issue an
139c6730ed4SJeremy Kerr 		 * isolated exit), we need to re-set it here */
140c6730ed4SJeremy Kerr 		runcntl = ctx->ops->runcntl_read(ctx) &
141c6730ed4SJeremy Kerr 			(SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
142c6730ed4SJeremy Kerr 		if (runcntl == 0)
143c6730ed4SJeremy Kerr 			runcntl = SPU_RUNCNTL_RUNNABLE;
144aa45e256SChristoph Hellwig 		ctx->ops->runcntl_write(ctx, runcntl);
1452eb1b120SChristoph Hellwig 	} else {
14605169237SBenjamin Herrenschmidt 		unsigned long mode = SPU_PRIVCNTL_MODE_NORMAL;
147c6730ed4SJeremy Kerr 		ctx->ops->npc_write(ctx, *npc);
14805169237SBenjamin Herrenschmidt 		if (test_thread_flag(TIF_SINGLESTEP))
14905169237SBenjamin Herrenschmidt 			mode = SPU_PRIVCNTL_MODE_SINGLE_STEP;
15005169237SBenjamin Herrenschmidt 		out_be64(&ctx->spu->priv2->spu_privcntl_RW, mode);
151aa45e256SChristoph Hellwig 		ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
1522eb1b120SChristoph Hellwig 	}
153c6730ed4SJeremy Kerr 
154aa45e256SChristoph Hellwig 	return 0;
155ce8ab854SArnd Bergmann }
156ce8ab854SArnd Bergmann 
157aa45e256SChristoph Hellwig static int spu_run_fini(struct spu_context *ctx, u32 * npc,
158ce8ab854SArnd Bergmann 			       u32 * status)
159ce8ab854SArnd Bergmann {
160ce8ab854SArnd Bergmann 	int ret = 0;
161ce8ab854SArnd Bergmann 
162ce8ab854SArnd Bergmann 	*status = ctx->ops->status_read(ctx);
163ce8ab854SArnd Bergmann 	*npc = ctx->ops->npc_read(ctx);
164ce8ab854SArnd Bergmann 	spu_release(ctx);
165ce8ab854SArnd Bergmann 
166ce8ab854SArnd Bergmann 	if (signal_pending(current))
167ce8ab854SArnd Bergmann 		ret = -ERESTARTSYS;
1682ebb2477SMasato Noguchi 
169ce8ab854SArnd Bergmann 	return ret;
170ce8ab854SArnd Bergmann }
171ce8ab854SArnd Bergmann 
172aa45e256SChristoph Hellwig static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
173ce8ab854SArnd Bergmann 				         u32 *status)
174ce8ab854SArnd Bergmann {
175ce8ab854SArnd Bergmann 	int ret;
176ce8ab854SArnd Bergmann 
177aa45e256SChristoph Hellwig 	ret = spu_run_fini(ctx, npc, status);
178aa45e256SChristoph Hellwig 	if (ret)
179ce8ab854SArnd Bergmann 		return ret;
180aa45e256SChristoph Hellwig 
181aa45e256SChristoph Hellwig 	if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
182ce8ab854SArnd Bergmann 		return *status;
183aa45e256SChristoph Hellwig 
184aa45e256SChristoph Hellwig 	ret = spu_acquire_runnable(ctx, 0);
185aa45e256SChristoph Hellwig 	if (ret)
186ce8ab854SArnd Bergmann 		return ret;
187aa45e256SChristoph Hellwig 
188aa45e256SChristoph Hellwig 	ret = spu_run_init(ctx, npc);
189aa45e256SChristoph Hellwig 	if (ret) {
190aa45e256SChristoph Hellwig 		spu_release(ctx);
191aa45e256SChristoph Hellwig 		return ret;
192aa45e256SChristoph Hellwig 	}
193ce8ab854SArnd Bergmann 	return 0;
194ce8ab854SArnd Bergmann }
195ce8ab854SArnd Bergmann 
1962dd14934SArnd Bergmann /*
1972dd14934SArnd Bergmann  * SPU syscall restarting is tricky because we violate the basic
1982dd14934SArnd Bergmann  * assumption that the signal handler is running on the interrupted
1992dd14934SArnd Bergmann  * thread. Here instead, the handler runs on PowerPC user space code,
2002dd14934SArnd Bergmann  * while the syscall was called from the SPU.
2012dd14934SArnd Bergmann  * This means we can only do a very rough approximation of POSIX
2022dd14934SArnd Bergmann  * signal semantics.
2032dd14934SArnd Bergmann  */
2042dd14934SArnd Bergmann int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
2052dd14934SArnd Bergmann 			  unsigned int *npc)
2062dd14934SArnd Bergmann {
2072dd14934SArnd Bergmann 	int ret;
2082dd14934SArnd Bergmann 
2092dd14934SArnd Bergmann 	switch (*spu_ret) {
2102dd14934SArnd Bergmann 	case -ERESTARTSYS:
2112dd14934SArnd Bergmann 	case -ERESTARTNOINTR:
2122dd14934SArnd Bergmann 		/*
2132dd14934SArnd Bergmann 		 * Enter the regular syscall restarting for
2142dd14934SArnd Bergmann 		 * sys_spu_run, then restart the SPU syscall
2152dd14934SArnd Bergmann 		 * callback.
2162dd14934SArnd Bergmann 		 */
2172dd14934SArnd Bergmann 		*npc -= 8;
2182dd14934SArnd Bergmann 		ret = -ERESTARTSYS;
2192dd14934SArnd Bergmann 		break;
2202dd14934SArnd Bergmann 	case -ERESTARTNOHAND:
2212dd14934SArnd Bergmann 	case -ERESTART_RESTARTBLOCK:
2222dd14934SArnd Bergmann 		/*
2232dd14934SArnd Bergmann 		 * Restart block is too hard for now, just return -EINTR
2242dd14934SArnd Bergmann 		 * to the SPU.
2252dd14934SArnd Bergmann 		 * ERESTARTNOHAND comes from sys_pause, we also return
2262dd14934SArnd Bergmann 		 * -EINTR from there.
2272dd14934SArnd Bergmann 		 * Assume that we need to be restarted ourselves though.
2282dd14934SArnd Bergmann 		 */
2292dd14934SArnd Bergmann 		*spu_ret = -EINTR;
2302dd14934SArnd Bergmann 		ret = -ERESTARTSYS;
2312dd14934SArnd Bergmann 		break;
2322dd14934SArnd Bergmann 	default:
2332dd14934SArnd Bergmann 		printk(KERN_WARNING "%s: unexpected return code %ld\n",
2342dd14934SArnd Bergmann 			__FUNCTION__, *spu_ret);
2352dd14934SArnd Bergmann 		ret = 0;
2362dd14934SArnd Bergmann 	}
2372dd14934SArnd Bergmann 	return ret;
2382dd14934SArnd Bergmann }
2392dd14934SArnd Bergmann 
2402dd14934SArnd Bergmann int spu_process_callback(struct spu_context *ctx)
2412dd14934SArnd Bergmann {
2422dd14934SArnd Bergmann 	struct spu_syscall_block s;
2432dd14934SArnd Bergmann 	u32 ls_pointer, npc;
2449e2fe2ceSAkinobu Mita 	void __iomem *ls;
2452dd14934SArnd Bergmann 	long spu_ret;
2462dd14934SArnd Bergmann 	int ret;
2472dd14934SArnd Bergmann 
2482dd14934SArnd Bergmann 	/* get syscall block from local store */
2499e2fe2ceSAkinobu Mita 	npc = ctx->ops->npc_read(ctx) & ~3;
2509e2fe2ceSAkinobu Mita 	ls = (void __iomem *)ctx->ops->get_ls(ctx);
2519e2fe2ceSAkinobu Mita 	ls_pointer = in_be32(ls + npc);
2522dd14934SArnd Bergmann 	if (ls_pointer > (LS_SIZE - sizeof(s)))
2532dd14934SArnd Bergmann 		return -EFAULT;
2549e2fe2ceSAkinobu Mita 	memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
2552dd14934SArnd Bergmann 
2562dd14934SArnd Bergmann 	/* do actual syscall without pinning the spu */
2572dd14934SArnd Bergmann 	ret = 0;
2582dd14934SArnd Bergmann 	spu_ret = -ENOSYS;
2592dd14934SArnd Bergmann 	npc += 4;
2602dd14934SArnd Bergmann 
2612dd14934SArnd Bergmann 	if (s.nr_ret < __NR_syscalls) {
2622dd14934SArnd Bergmann 		spu_release(ctx);
2632dd14934SArnd Bergmann 		/* do actual system call from here */
2642dd14934SArnd Bergmann 		spu_ret = spu_sys_callback(&s);
2652dd14934SArnd Bergmann 		if (spu_ret <= -ERESTARTSYS) {
2662dd14934SArnd Bergmann 			ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
2672dd14934SArnd Bergmann 		}
2682dd14934SArnd Bergmann 		spu_acquire(ctx);
2692dd14934SArnd Bergmann 		if (ret == -ERESTARTSYS)
2702dd14934SArnd Bergmann 			return ret;
2712dd14934SArnd Bergmann 	}
2722dd14934SArnd Bergmann 
2732dd14934SArnd Bergmann 	/* write result, jump over indirect pointer */
2749e2fe2ceSAkinobu Mita 	memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
2752dd14934SArnd Bergmann 	ctx->ops->npc_write(ctx, npc);
2762dd14934SArnd Bergmann 	ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
2772dd14934SArnd Bergmann 	return ret;
2782dd14934SArnd Bergmann }
2792dd14934SArnd Bergmann 
280ce8ab854SArnd Bergmann static inline int spu_process_events(struct spu_context *ctx)
281ce8ab854SArnd Bergmann {
282ce8ab854SArnd Bergmann 	struct spu *spu = ctx->spu;
283ce8ab854SArnd Bergmann 	int ret = 0;
284ce8ab854SArnd Bergmann 
285ce8ab854SArnd Bergmann 	if (spu->class_0_pending)
286ce8ab854SArnd Bergmann 		ret = spu_irq_class_0_bottom(spu);
287ce8ab854SArnd Bergmann 	if (!ret && signal_pending(current))
288ce8ab854SArnd Bergmann 		ret = -ERESTARTSYS;
289ce8ab854SArnd Bergmann 	return ret;
290ce8ab854SArnd Bergmann }
291ce8ab854SArnd Bergmann 
292ce8ab854SArnd Bergmann long spufs_run_spu(struct file *file, struct spu_context *ctx,
2939add11daSArnd Bergmann 		   u32 *npc, u32 *event)
294ce8ab854SArnd Bergmann {
295ce8ab854SArnd Bergmann 	int ret;
2969add11daSArnd Bergmann 	u32 status;
297ce8ab854SArnd Bergmann 
298e45d48a3SChristoph Hellwig 	if (mutex_lock_interruptible(&ctx->run_mutex))
299ce8ab854SArnd Bergmann 		return -ERESTARTSYS;
300ce8ab854SArnd Bergmann 
301ee2d7340SArnd Bergmann 	ctx->ops->master_start(ctx);
3029add11daSArnd Bergmann 	ctx->event_return = 0;
303aa45e256SChristoph Hellwig 
3042cf2b3b4SChristoph Hellwig 	spu_acquire(ctx);
3052cf2b3b4SChristoph Hellwig 	if (ctx->state == SPU_STATE_SAVED) {
3062cf2b3b4SChristoph Hellwig 		__spu_update_sched_info(ctx);
3072cf2b3b4SChristoph Hellwig 
3082cf2b3b4SChristoph Hellwig 		ret = spu_activate(ctx, 0);
3092cf2b3b4SChristoph Hellwig 		if (ret) {
3102cf2b3b4SChristoph Hellwig 			spu_release(ctx);
3112cf2b3b4SChristoph Hellwig 			goto out;
3122cf2b3b4SChristoph Hellwig 		}
3132cf2b3b4SChristoph Hellwig 	} else {
3142cf2b3b4SChristoph Hellwig 		/*
3152cf2b3b4SChristoph Hellwig 		 * We have to update the scheduling priority under active_mutex
3162cf2b3b4SChristoph Hellwig 		 * to protect against find_victim().
3172cf2b3b4SChristoph Hellwig 		 */
3182cf2b3b4SChristoph Hellwig 		spu_update_sched_info(ctx);
3192cf2b3b4SChristoph Hellwig 	}
320aa45e256SChristoph Hellwig 
321aa45e256SChristoph Hellwig 	ret = spu_run_init(ctx, npc);
322aa45e256SChristoph Hellwig 	if (ret) {
323aa45e256SChristoph Hellwig 		spu_release(ctx);
324ce8ab854SArnd Bergmann 		goto out;
325aa45e256SChristoph Hellwig 	}
326ce8ab854SArnd Bergmann 
327ce8ab854SArnd Bergmann 	do {
3289add11daSArnd Bergmann 		ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
329ce8ab854SArnd Bergmann 		if (unlikely(ret))
330ce8ab854SArnd Bergmann 			break;
3319add11daSArnd Bergmann 		if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
3329add11daSArnd Bergmann 		    (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
3332dd14934SArnd Bergmann 			ret = spu_process_callback(ctx);
3342dd14934SArnd Bergmann 			if (ret)
3352dd14934SArnd Bergmann 				break;
3369add11daSArnd Bergmann 			status &= ~SPU_STATUS_STOPPED_BY_STOP;
3372dd14934SArnd Bergmann 		}
33857dace23SArnd Bergmann 		ret = spufs_handle_class1(ctx);
33957dace23SArnd Bergmann 		if (ret)
34057dace23SArnd Bergmann 			break;
34157dace23SArnd Bergmann 
342ce8ab854SArnd Bergmann 		if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
3439add11daSArnd Bergmann 			ret = spu_reacquire_runnable(ctx, npc, &status);
34437901802SChristoph Hellwig 			if (ret)
3452ebb2477SMasato Noguchi 				goto out2;
346ce8ab854SArnd Bergmann 			continue;
347ce8ab854SArnd Bergmann 		}
348ce8ab854SArnd Bergmann 		ret = spu_process_events(ctx);
349ce8ab854SArnd Bergmann 
3509add11daSArnd Bergmann 	} while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
35105169237SBenjamin Herrenschmidt 				      SPU_STATUS_STOPPED_BY_HALT |
35205169237SBenjamin Herrenschmidt 				       SPU_STATUS_SINGLE_STEP)));
353ce8ab854SArnd Bergmann 
354e9f8a0b6SChristoph Hellwig 	if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
355e9f8a0b6SChristoph Hellwig 	    ((status >> SPU_STOP_STATUS_SHIFT) & 0x2100))
356e9f8a0b6SChristoph Hellwig 		ctx->stats.libassist++;
357e9f8a0b6SChristoph Hellwig 
358ee2d7340SArnd Bergmann 	ctx->ops->master_stop(ctx);
3599add11daSArnd Bergmann 	ret = spu_run_fini(ctx, npc, &status);
360ce8ab854SArnd Bergmann 	spu_yield(ctx);
361ce8ab854SArnd Bergmann 
3622ebb2477SMasato Noguchi out2:
3632ebb2477SMasato Noguchi 	if ((ret == 0) ||
3642ebb2477SMasato Noguchi 	    ((ret == -ERESTARTSYS) &&
3652ebb2477SMasato Noguchi 	     ((status & SPU_STATUS_STOPPED_BY_HALT) ||
36605169237SBenjamin Herrenschmidt 	      (status & SPU_STATUS_SINGLE_STEP) ||
3672ebb2477SMasato Noguchi 	      ((status & SPU_STATUS_STOPPED_BY_STOP) &&
3682ebb2477SMasato Noguchi 	       (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
3692ebb2477SMasato Noguchi 		ret = status;
3702ebb2477SMasato Noguchi 
37105169237SBenjamin Herrenschmidt 	/* Note: we don't need to force_sig SIGTRAP on single-step
37205169237SBenjamin Herrenschmidt 	 * since we have TIF_SINGLESTEP set, thus the kernel will do
37305169237SBenjamin Herrenschmidt 	 * it upon return from the syscall anyawy
37405169237SBenjamin Herrenschmidt 	 */
3752ebb2477SMasato Noguchi 	if ((status & SPU_STATUS_STOPPED_BY_STOP)
3762ebb2477SMasato Noguchi 	    && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
3772ebb2477SMasato Noguchi 		force_sig(SIGTRAP, current);
3782ebb2477SMasato Noguchi 		ret = -ERESTARTSYS;
3792ebb2477SMasato Noguchi 	}
3802ebb2477SMasato Noguchi 
381ce8ab854SArnd Bergmann out:
3829add11daSArnd Bergmann 	*event = ctx->event_return;
383e45d48a3SChristoph Hellwig 	mutex_unlock(&ctx->run_mutex);
384ce8ab854SArnd Bergmann 	return ret;
385ce8ab854SArnd Bergmann }
386