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);
2736aaccc1SBob Nelson 
28ce8ab854SArnd Bergmann 	spu = ctx->spu;
2936aaccc1SBob Nelson 	if (ctx->state != SPU_STATE_RUNNABLE ||
3036aaccc1SBob Nelson 	    test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags))
3136aaccc1SBob Nelson 		return 1;
32ce8ab854SArnd Bergmann 	pte_fault = spu->dsisr &
33ce8ab854SArnd Bergmann 	    (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
34be703177SSebastian Siewior 	return (!(*stat & SPU_STATUS_RUNNING) || pte_fault || spu->class_0_pending) ?
35be703177SSebastian Siewior 		1 : 0;
36ce8ab854SArnd Bergmann }
37ce8ab854SArnd Bergmann 
38c6730ed4SJeremy Kerr static int spu_setup_isolated(struct spu_context *ctx)
39c6730ed4SJeremy Kerr {
40c6730ed4SJeremy Kerr 	int ret;
41c6730ed4SJeremy Kerr 	u64 __iomem *mfc_cntl;
42c6730ed4SJeremy Kerr 	u64 sr1;
43c6730ed4SJeremy Kerr 	u32 status;
44c6730ed4SJeremy Kerr 	unsigned long timeout;
45c6730ed4SJeremy Kerr 	const u32 status_loading = SPU_STATUS_RUNNING
46c6730ed4SJeremy Kerr 		| SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
47c6730ed4SJeremy Kerr 
487ec18ab9SChristoph Hellwig 	ret = -ENODEV;
49c6730ed4SJeremy Kerr 	if (!isolated_loader)
50c6730ed4SJeremy Kerr 		goto out;
51c6730ed4SJeremy Kerr 
527ec18ab9SChristoph Hellwig 	/*
537ec18ab9SChristoph Hellwig 	 * We need to exclude userspace access to the context.
547ec18ab9SChristoph Hellwig 	 *
557ec18ab9SChristoph Hellwig 	 * To protect against memory access we invalidate all ptes
567ec18ab9SChristoph Hellwig 	 * and make sure the pagefault handlers block on the mutex.
577ec18ab9SChristoph Hellwig 	 */
587ec18ab9SChristoph Hellwig 	spu_unmap_mappings(ctx);
597ec18ab9SChristoph Hellwig 
60c6730ed4SJeremy Kerr 	mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
61c6730ed4SJeremy Kerr 
62c6730ed4SJeremy Kerr 	/* purge the MFC DMA queue to ensure no spurious accesses before we
63c6730ed4SJeremy Kerr 	 * enter kernel mode */
64c6730ed4SJeremy Kerr 	timeout = jiffies + HZ;
65c6730ed4SJeremy Kerr 	out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
66c6730ed4SJeremy Kerr 	while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
67c6730ed4SJeremy Kerr 			!= MFC_CNTL_PURGE_DMA_COMPLETE) {
68c6730ed4SJeremy Kerr 		if (time_after(jiffies, timeout)) {
69c6730ed4SJeremy Kerr 			printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
70c6730ed4SJeremy Kerr 					__FUNCTION__);
71c6730ed4SJeremy Kerr 			ret = -EIO;
727ec18ab9SChristoph Hellwig 			goto out;
73c6730ed4SJeremy Kerr 		}
74c6730ed4SJeremy Kerr 		cond_resched();
75c6730ed4SJeremy Kerr 	}
76c6730ed4SJeremy Kerr 
77c6730ed4SJeremy Kerr 	/* put the SPE in kernel mode to allow access to the loader */
78c6730ed4SJeremy Kerr 	sr1 = spu_mfc_sr1_get(ctx->spu);
79c6730ed4SJeremy Kerr 	sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
80c6730ed4SJeremy Kerr 	spu_mfc_sr1_set(ctx->spu, sr1);
81c6730ed4SJeremy Kerr 
82c6730ed4SJeremy Kerr 	/* start the loader */
83c6730ed4SJeremy Kerr 	ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
84c6730ed4SJeremy Kerr 	ctx->ops->signal2_write(ctx,
85c6730ed4SJeremy Kerr 			(unsigned long)isolated_loader & 0xffffffff);
86c6730ed4SJeremy Kerr 
87c6730ed4SJeremy Kerr 	ctx->ops->runcntl_write(ctx,
88c6730ed4SJeremy Kerr 			SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
89c6730ed4SJeremy Kerr 
90c6730ed4SJeremy Kerr 	ret = 0;
91c6730ed4SJeremy Kerr 	timeout = jiffies + HZ;
92c6730ed4SJeremy Kerr 	while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
93c6730ed4SJeremy Kerr 				status_loading) {
94c6730ed4SJeremy Kerr 		if (time_after(jiffies, timeout)) {
95c6730ed4SJeremy Kerr 			printk(KERN_ERR "%s: timeout waiting for loader\n",
96c6730ed4SJeremy Kerr 					__FUNCTION__);
97c6730ed4SJeremy Kerr 			ret = -EIO;
98c6730ed4SJeremy Kerr 			goto out_drop_priv;
99c6730ed4SJeremy Kerr 		}
100c6730ed4SJeremy Kerr 		cond_resched();
101c6730ed4SJeremy Kerr 	}
102c6730ed4SJeremy Kerr 
103c6730ed4SJeremy Kerr 	if (!(status & SPU_STATUS_RUNNING)) {
104c6730ed4SJeremy Kerr 		/* If isolated LOAD has failed: run SPU, we will get a stop-and
105c6730ed4SJeremy Kerr 		 * signal later. */
106c6730ed4SJeremy Kerr 		pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
107c6730ed4SJeremy Kerr 		ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
108c6730ed4SJeremy Kerr 		ret = -EACCES;
1097ec18ab9SChristoph Hellwig 		goto out_drop_priv;
1107ec18ab9SChristoph Hellwig 	}
111c6730ed4SJeremy Kerr 
1127ec18ab9SChristoph Hellwig 	if (!(status & SPU_STATUS_ISOLATED_STATE)) {
113c6730ed4SJeremy Kerr 		/* This isn't allowed by the CBEA, but check anyway */
114c6730ed4SJeremy Kerr 		pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
115c6730ed4SJeremy Kerr 		ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
116c6730ed4SJeremy Kerr 		ret = -EINVAL;
1177ec18ab9SChristoph Hellwig 		goto out_drop_priv;
118c6730ed4SJeremy Kerr 	}
119c6730ed4SJeremy Kerr 
120c6730ed4SJeremy Kerr out_drop_priv:
121c6730ed4SJeremy Kerr 	/* Finished accessing the loader. Drop kernel mode */
122c6730ed4SJeremy Kerr 	sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
123c6730ed4SJeremy Kerr 	spu_mfc_sr1_set(ctx->spu, sr1);
124c6730ed4SJeremy Kerr 
125c6730ed4SJeremy Kerr out:
126c6730ed4SJeremy Kerr 	return ret;
127c6730ed4SJeremy Kerr }
128c6730ed4SJeremy Kerr 
129aa45e256SChristoph Hellwig static int spu_run_init(struct spu_context *ctx, u32 *npc)
130ce8ab854SArnd Bergmann {
13127ec41d3SAndre Detsch 	spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
13227ec41d3SAndre Detsch 
133aa45e256SChristoph Hellwig 	if (ctx->flags & SPU_CREATE_ISOLATE) {
134aa45e256SChristoph Hellwig 		unsigned long runcntl;
135ce8ab854SArnd Bergmann 
136aa45e256SChristoph Hellwig 		if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
137aa45e256SChristoph Hellwig 			int ret = spu_setup_isolated(ctx);
138c6730ed4SJeremy Kerr 			if (ret)
139ce8ab854SArnd Bergmann 				return ret;
1400afacde3Sarnd@arndb.de 		}
141c6730ed4SJeremy Kerr 
142c6730ed4SJeremy Kerr 		/* if userspace has set the runcntrl register (eg, to issue an
143c6730ed4SJeremy Kerr 		 * isolated exit), we need to re-set it here */
144c6730ed4SJeremy Kerr 		runcntl = ctx->ops->runcntl_read(ctx) &
145c6730ed4SJeremy Kerr 			(SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
146c6730ed4SJeremy Kerr 		if (runcntl == 0)
147c6730ed4SJeremy Kerr 			runcntl = SPU_RUNCNTL_RUNNABLE;
148aa45e256SChristoph Hellwig 		ctx->ops->runcntl_write(ctx, runcntl);
1492eb1b120SChristoph Hellwig 	} else {
15005169237SBenjamin Herrenschmidt 		unsigned long mode = SPU_PRIVCNTL_MODE_NORMAL;
151c6730ed4SJeremy Kerr 		ctx->ops->npc_write(ctx, *npc);
15205169237SBenjamin Herrenschmidt 		if (test_thread_flag(TIF_SINGLESTEP))
15305169237SBenjamin Herrenschmidt 			mode = SPU_PRIVCNTL_MODE_SINGLE_STEP;
15405169237SBenjamin Herrenschmidt 		out_be64(&ctx->spu->priv2->spu_privcntl_RW, mode);
155aa45e256SChristoph Hellwig 		ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
1562eb1b120SChristoph Hellwig 	}
157c6730ed4SJeremy Kerr 
15827ec41d3SAndre Detsch 	spuctx_switch_state(ctx, SPU_UTIL_USER);
15927ec41d3SAndre Detsch 
160aa45e256SChristoph Hellwig 	return 0;
161ce8ab854SArnd Bergmann }
162ce8ab854SArnd Bergmann 
163aa45e256SChristoph Hellwig static int spu_run_fini(struct spu_context *ctx, u32 *npc,
164ce8ab854SArnd Bergmann 			       u32 *status)
165ce8ab854SArnd Bergmann {
166ce8ab854SArnd Bergmann 	int ret = 0;
167ce8ab854SArnd Bergmann 
168ce8ab854SArnd Bergmann 	*status = ctx->ops->status_read(ctx);
169ce8ab854SArnd Bergmann 	*npc = ctx->ops->npc_read(ctx);
17027ec41d3SAndre Detsch 
17127ec41d3SAndre Detsch 	spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
172ce8ab854SArnd Bergmann 	spu_release(ctx);
173ce8ab854SArnd Bergmann 
174ce8ab854SArnd Bergmann 	if (signal_pending(current))
175ce8ab854SArnd Bergmann 		ret = -ERESTARTSYS;
1762ebb2477SMasato Noguchi 
177ce8ab854SArnd Bergmann 	return ret;
178ce8ab854SArnd Bergmann }
179ce8ab854SArnd Bergmann 
180aa45e256SChristoph Hellwig static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
181ce8ab854SArnd Bergmann 				         u32 *status)
182ce8ab854SArnd Bergmann {
183ce8ab854SArnd Bergmann 	int ret;
184ce8ab854SArnd Bergmann 
185aa45e256SChristoph Hellwig 	ret = spu_run_fini(ctx, npc, status);
186aa45e256SChristoph Hellwig 	if (ret)
187ce8ab854SArnd Bergmann 		return ret;
188aa45e256SChristoph Hellwig 
189aa45e256SChristoph Hellwig 	if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
190ce8ab854SArnd Bergmann 		return *status;
191aa45e256SChristoph Hellwig 
192aa45e256SChristoph Hellwig 	ret = spu_acquire_runnable(ctx, 0);
193aa45e256SChristoph Hellwig 	if (ret)
194ce8ab854SArnd Bergmann 		return ret;
195aa45e256SChristoph Hellwig 
196ada83daaSAndre Detsch 	spuctx_switch_state(ctx, SPU_UTIL_USER);
197ce8ab854SArnd Bergmann 	return 0;
198ce8ab854SArnd Bergmann }
199ce8ab854SArnd Bergmann 
2002dd14934SArnd Bergmann /*
2012dd14934SArnd Bergmann  * SPU syscall restarting is tricky because we violate the basic
2022dd14934SArnd Bergmann  * assumption that the signal handler is running on the interrupted
2032dd14934SArnd Bergmann  * thread. Here instead, the handler runs on PowerPC user space code,
2042dd14934SArnd Bergmann  * while the syscall was called from the SPU.
2052dd14934SArnd Bergmann  * This means we can only do a very rough approximation of POSIX
2062dd14934SArnd Bergmann  * signal semantics.
2072dd14934SArnd Bergmann  */
2081238819aSSebastian Siewior static int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
2092dd14934SArnd Bergmann 			  unsigned int *npc)
2102dd14934SArnd Bergmann {
2112dd14934SArnd Bergmann 	int ret;
2122dd14934SArnd Bergmann 
2132dd14934SArnd Bergmann 	switch (*spu_ret) {
2142dd14934SArnd Bergmann 	case -ERESTARTSYS:
2152dd14934SArnd Bergmann 	case -ERESTARTNOINTR:
2162dd14934SArnd Bergmann 		/*
2172dd14934SArnd Bergmann 		 * Enter the regular syscall restarting for
2182dd14934SArnd Bergmann 		 * sys_spu_run, then restart the SPU syscall
2192dd14934SArnd Bergmann 		 * callback.
2202dd14934SArnd Bergmann 		 */
2212dd14934SArnd Bergmann 		*npc -= 8;
2222dd14934SArnd Bergmann 		ret = -ERESTARTSYS;
2232dd14934SArnd Bergmann 		break;
2242dd14934SArnd Bergmann 	case -ERESTARTNOHAND:
2252dd14934SArnd Bergmann 	case -ERESTART_RESTARTBLOCK:
2262dd14934SArnd Bergmann 		/*
2272dd14934SArnd Bergmann 		 * Restart block is too hard for now, just return -EINTR
2282dd14934SArnd Bergmann 		 * to the SPU.
2292dd14934SArnd Bergmann 		 * ERESTARTNOHAND comes from sys_pause, we also return
2302dd14934SArnd Bergmann 		 * -EINTR from there.
2312dd14934SArnd Bergmann 		 * Assume that we need to be restarted ourselves though.
2322dd14934SArnd Bergmann 		 */
2332dd14934SArnd Bergmann 		*spu_ret = -EINTR;
2342dd14934SArnd Bergmann 		ret = -ERESTARTSYS;
2352dd14934SArnd Bergmann 		break;
2362dd14934SArnd Bergmann 	default:
2372dd14934SArnd Bergmann 		printk(KERN_WARNING "%s: unexpected return code %ld\n",
2382dd14934SArnd Bergmann 			__FUNCTION__, *spu_ret);
2392dd14934SArnd Bergmann 		ret = 0;
2402dd14934SArnd Bergmann 	}
2412dd14934SArnd Bergmann 	return ret;
2422dd14934SArnd Bergmann }
2432dd14934SArnd Bergmann 
2441238819aSSebastian Siewior static int spu_process_callback(struct spu_context *ctx)
2452dd14934SArnd Bergmann {
2462dd14934SArnd Bergmann 	struct spu_syscall_block s;
2472dd14934SArnd Bergmann 	u32 ls_pointer, npc;
2489e2fe2ceSAkinobu Mita 	void __iomem *ls;
2492dd14934SArnd Bergmann 	long spu_ret;
2502dd14934SArnd Bergmann 	int ret;
2512dd14934SArnd Bergmann 
2522dd14934SArnd Bergmann 	/* get syscall block from local store */
2539e2fe2ceSAkinobu Mita 	npc = ctx->ops->npc_read(ctx) & ~3;
2549e2fe2ceSAkinobu Mita 	ls = (void __iomem *)ctx->ops->get_ls(ctx);
2559e2fe2ceSAkinobu Mita 	ls_pointer = in_be32(ls + npc);
2562dd14934SArnd Bergmann 	if (ls_pointer > (LS_SIZE - sizeof(s)))
2572dd14934SArnd Bergmann 		return -EFAULT;
2589e2fe2ceSAkinobu Mita 	memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
2592dd14934SArnd Bergmann 
2602dd14934SArnd Bergmann 	/* do actual syscall without pinning the spu */
2612dd14934SArnd Bergmann 	ret = 0;
2622dd14934SArnd Bergmann 	spu_ret = -ENOSYS;
2632dd14934SArnd Bergmann 	npc += 4;
2642dd14934SArnd Bergmann 
2652dd14934SArnd Bergmann 	if (s.nr_ret < __NR_syscalls) {
2662dd14934SArnd Bergmann 		spu_release(ctx);
2672dd14934SArnd Bergmann 		/* do actual system call from here */
2682dd14934SArnd Bergmann 		spu_ret = spu_sys_callback(&s);
2692dd14934SArnd Bergmann 		if (spu_ret <= -ERESTARTSYS) {
2702dd14934SArnd Bergmann 			ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
2712dd14934SArnd Bergmann 		}
2722dd14934SArnd Bergmann 		spu_acquire(ctx);
2732dd14934SArnd Bergmann 		if (ret == -ERESTARTSYS)
2742dd14934SArnd Bergmann 			return ret;
2752dd14934SArnd Bergmann 	}
2762dd14934SArnd Bergmann 
2772dd14934SArnd Bergmann 	/* write result, jump over indirect pointer */
2789e2fe2ceSAkinobu Mita 	memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
2792dd14934SArnd Bergmann 	ctx->ops->npc_write(ctx, npc);
2802dd14934SArnd Bergmann 	ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
2812dd14934SArnd Bergmann 	return ret;
2822dd14934SArnd Bergmann }
2832dd14934SArnd Bergmann 
284ce8ab854SArnd Bergmann static inline int spu_process_events(struct spu_context *ctx)
285ce8ab854SArnd Bergmann {
286ce8ab854SArnd Bergmann 	struct spu *spu = ctx->spu;
287ce8ab854SArnd Bergmann 	int ret = 0;
288ce8ab854SArnd Bergmann 
289ce8ab854SArnd Bergmann 	if (spu->class_0_pending)
290ce8ab854SArnd Bergmann 		ret = spu_irq_class_0_bottom(spu);
291ce8ab854SArnd Bergmann 	if (!ret && signal_pending(current))
292ce8ab854SArnd Bergmann 		ret = -ERESTARTSYS;
293ce8ab854SArnd Bergmann 	return ret;
294ce8ab854SArnd Bergmann }
295ce8ab854SArnd Bergmann 
29650af32a9SJeremy Kerr long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event)
297ce8ab854SArnd Bergmann {
298ce8ab854SArnd Bergmann 	int ret;
29936aaccc1SBob Nelson 	struct spu *spu;
3009add11daSArnd Bergmann 	u32 status;
301ce8ab854SArnd Bergmann 
302e45d48a3SChristoph Hellwig 	if (mutex_lock_interruptible(&ctx->run_mutex))
303ce8ab854SArnd Bergmann 		return -ERESTARTSYS;
304ce8ab854SArnd Bergmann 
305c25620d7SMasato Noguchi 	spu_enable_spu(ctx);
3069add11daSArnd Bergmann 	ctx->event_return = 0;
307aa45e256SChristoph Hellwig 
3082cf2b3b4SChristoph Hellwig 	spu_acquire(ctx);
3092cf2b3b4SChristoph Hellwig 	if (ctx->state == SPU_STATE_SAVED) {
3102cf2b3b4SChristoph Hellwig 		__spu_update_sched_info(ctx);
3119d78592eSChristoph Hellwig 		spu_set_timeslice(ctx);
3122cf2b3b4SChristoph Hellwig 
3132cf2b3b4SChristoph Hellwig 		ret = spu_activate(ctx, 0);
3142cf2b3b4SChristoph Hellwig 		if (ret) {
3152cf2b3b4SChristoph Hellwig 			spu_release(ctx);
3162cf2b3b4SChristoph Hellwig 			goto out;
3172cf2b3b4SChristoph Hellwig 		}
3182cf2b3b4SChristoph Hellwig 	} else {
3192cf2b3b4SChristoph Hellwig 		/*
3202cf2b3b4SChristoph Hellwig 		 * We have to update the scheduling priority under active_mutex
3212cf2b3b4SChristoph Hellwig 		 * to protect against find_victim().
3229d78592eSChristoph Hellwig 		 *
3239d78592eSChristoph Hellwig 		 * No need to update the timeslice ASAP, it will get updated
3249d78592eSChristoph Hellwig 		 * once the current one has expired.
3252cf2b3b4SChristoph Hellwig 		 */
3262cf2b3b4SChristoph Hellwig 		spu_update_sched_info(ctx);
3272cf2b3b4SChristoph Hellwig 	}
328aa45e256SChristoph Hellwig 
329aa45e256SChristoph Hellwig 	ret = spu_run_init(ctx, npc);
330aa45e256SChristoph Hellwig 	if (ret) {
331aa45e256SChristoph Hellwig 		spu_release(ctx);
332ce8ab854SArnd Bergmann 		goto out;
333aa45e256SChristoph Hellwig 	}
334ce8ab854SArnd Bergmann 
335ce8ab854SArnd Bergmann 	do {
3369add11daSArnd Bergmann 		ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
337ce8ab854SArnd Bergmann 		if (unlikely(ret))
338ce8ab854SArnd Bergmann 			break;
33936aaccc1SBob Nelson 		spu = ctx->spu;
34036aaccc1SBob Nelson 		if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE,
34136aaccc1SBob Nelson 						&ctx->sched_flags))) {
34236aaccc1SBob Nelson 			if (!(status & SPU_STATUS_STOPPED_BY_STOP)) {
34336aaccc1SBob Nelson 				spu_switch_notify(spu, ctx);
34436aaccc1SBob Nelson 				continue;
34536aaccc1SBob Nelson 			}
34636aaccc1SBob Nelson 		}
34727ec41d3SAndre Detsch 
34827ec41d3SAndre Detsch 		spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
34927ec41d3SAndre Detsch 
3509add11daSArnd Bergmann 		if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
3519add11daSArnd Bergmann 		    (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
3522dd14934SArnd Bergmann 			ret = spu_process_callback(ctx);
3532dd14934SArnd Bergmann 			if (ret)
3542dd14934SArnd Bergmann 				break;
3559add11daSArnd Bergmann 			status &= ~SPU_STATUS_STOPPED_BY_STOP;
3562dd14934SArnd Bergmann 		}
35757dace23SArnd Bergmann 		ret = spufs_handle_class1(ctx);
35857dace23SArnd Bergmann 		if (ret)
35957dace23SArnd Bergmann 			break;
36057dace23SArnd Bergmann 
361ce8ab854SArnd Bergmann 		if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
3629add11daSArnd Bergmann 			ret = spu_reacquire_runnable(ctx, npc, &status);
36337901802SChristoph Hellwig 			if (ret)
3642ebb2477SMasato Noguchi 				goto out2;
365ce8ab854SArnd Bergmann 			continue;
366ce8ab854SArnd Bergmann 		}
367ce8ab854SArnd Bergmann 		ret = spu_process_events(ctx);
368ce8ab854SArnd Bergmann 
3699add11daSArnd Bergmann 	} while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
37005169237SBenjamin Herrenschmidt 				      SPU_STATUS_STOPPED_BY_HALT |
37105169237SBenjamin Herrenschmidt 				       SPU_STATUS_SINGLE_STEP)));
372ce8ab854SArnd Bergmann 
373e9f8a0b6SChristoph Hellwig 	if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
374fe2f896dSChristoph Hellwig 	    (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100) &&
375fe2f896dSChristoph Hellwig 	    (ctx->state == SPU_STATE_RUNNABLE))
376e9f8a0b6SChristoph Hellwig 		ctx->stats.libassist++;
377e9f8a0b6SChristoph Hellwig 
37827ec41d3SAndre Detsch 
379c25620d7SMasato Noguchi 	spu_disable_spu(ctx);
3809add11daSArnd Bergmann 	ret = spu_run_fini(ctx, npc, &status);
381ce8ab854SArnd Bergmann 	spu_yield(ctx);
382ce8ab854SArnd Bergmann 
3832ebb2477SMasato Noguchi out2:
3842ebb2477SMasato Noguchi 	if ((ret == 0) ||
3852ebb2477SMasato Noguchi 	    ((ret == -ERESTARTSYS) &&
3862ebb2477SMasato Noguchi 	     ((status & SPU_STATUS_STOPPED_BY_HALT) ||
38705169237SBenjamin Herrenschmidt 	      (status & SPU_STATUS_SINGLE_STEP) ||
3882ebb2477SMasato Noguchi 	      ((status & SPU_STATUS_STOPPED_BY_STOP) &&
3892ebb2477SMasato Noguchi 	       (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
3902ebb2477SMasato Noguchi 		ret = status;
3912ebb2477SMasato Noguchi 
39205169237SBenjamin Herrenschmidt 	/* Note: we don't need to force_sig SIGTRAP on single-step
39305169237SBenjamin Herrenschmidt 	 * since we have TIF_SINGLESTEP set, thus the kernel will do
39405169237SBenjamin Herrenschmidt 	 * it upon return from the syscall anyawy
39505169237SBenjamin Herrenschmidt 	 */
3962ebb2477SMasato Noguchi 	if ((status & SPU_STATUS_STOPPED_BY_STOP)
3972ebb2477SMasato Noguchi 	    && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
3982ebb2477SMasato Noguchi 		force_sig(SIGTRAP, current);
3992ebb2477SMasato Noguchi 		ret = -ERESTARTSYS;
4002ebb2477SMasato Noguchi 	}
4012ebb2477SMasato Noguchi 
402ce8ab854SArnd Bergmann out:
4039add11daSArnd Bergmann 	*event = ctx->event_return;
404e45d48a3SChristoph Hellwig 	mutex_unlock(&ctx->run_mutex);
405ce8ab854SArnd Bergmann 	return ret;
406ce8ab854SArnd Bergmann }
407