run.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #define DEBUG
  2. #include <linux/wait.h>
  3. #include <linux/ptrace.h>
  4. #include <asm/spu.h>
  5. #include <asm/spu_priv1.h>
  6. #include <asm/io.h>
  7. #include <asm/unistd.h>
  8. #include "spufs.h"
  9. /* interrupt-level stop callback function. */
  10. void spufs_stop_callback(struct spu *spu, int irq)
  11. {
  12. struct spu_context *ctx = spu->ctx;
  13. /*
  14. * It should be impossible to preempt a context while an exception
  15. * is being processed, since the context switch code is specially
  16. * coded to deal with interrupts ... But, just in case, sanity check
  17. * the context pointer. It is OK to return doing nothing since
  18. * the exception will be regenerated when the context is resumed.
  19. */
  20. if (ctx) {
  21. /* Copy exception arguments into module specific structure */
  22. switch(irq) {
  23. case 0 :
  24. ctx->csa.class_0_pending = spu->class_0_pending;
  25. ctx->csa.class_0_dsisr = spu->class_0_dsisr;
  26. ctx->csa.class_0_dar = spu->class_0_dar;
  27. break;
  28. case 1 :
  29. ctx->csa.class_1_dsisr = spu->class_1_dsisr;
  30. ctx->csa.class_1_dar = spu->class_1_dar;
  31. break;
  32. case 2 :
  33. break;
  34. }
  35. /* ensure that the exception status has hit memory before a
  36. * thread waiting on the context's stop queue is woken */
  37. smp_wmb();
  38. wake_up_all(&ctx->stop_wq);
  39. }
  40. }
  41. int spu_stopped(struct spu_context *ctx, u32 *stat)
  42. {
  43. u64 dsisr;
  44. u32 stopped;
  45. *stat = ctx->ops->status_read(ctx);
  46. if (test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags))
  47. return 1;
  48. stopped = SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
  49. SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
  50. if (!(*stat & SPU_STATUS_RUNNING) && (*stat & stopped))
  51. return 1;
  52. dsisr = ctx->csa.class_0_dsisr;
  53. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED))
  54. return 1;
  55. dsisr = ctx->csa.class_1_dsisr;
  56. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED))
  57. return 1;
  58. if (ctx->csa.class_0_pending)
  59. return 1;
  60. return 0;
  61. }
  62. static int spu_setup_isolated(struct spu_context *ctx)
  63. {
  64. int ret;
  65. u64 __iomem *mfc_cntl;
  66. u64 sr1;
  67. u32 status;
  68. unsigned long timeout;
  69. const u32 status_loading = SPU_STATUS_RUNNING
  70. | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
  71. ret = -ENODEV;
  72. if (!isolated_loader)
  73. goto out;
  74. /*
  75. * We need to exclude userspace access to the context.
  76. *
  77. * To protect against memory access we invalidate all ptes
  78. * and make sure the pagefault handlers block on the mutex.
  79. */
  80. spu_unmap_mappings(ctx);
  81. mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
  82. /* purge the MFC DMA queue to ensure no spurious accesses before we
  83. * enter kernel mode */
  84. timeout = jiffies + HZ;
  85. out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
  86. while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
  87. != MFC_CNTL_PURGE_DMA_COMPLETE) {
  88. if (time_after(jiffies, timeout)) {
  89. printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
  90. __func__);
  91. ret = -EIO;
  92. goto out;
  93. }
  94. cond_resched();
  95. }
  96. /* put the SPE in kernel mode to allow access to the loader */
  97. sr1 = spu_mfc_sr1_get(ctx->spu);
  98. sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
  99. spu_mfc_sr1_set(ctx->spu, sr1);
  100. /* start the loader */
  101. ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
  102. ctx->ops->signal2_write(ctx,
  103. (unsigned long)isolated_loader & 0xffffffff);
  104. ctx->ops->runcntl_write(ctx,
  105. SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  106. ret = 0;
  107. timeout = jiffies + HZ;
  108. while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
  109. status_loading) {
  110. if (time_after(jiffies, timeout)) {
  111. printk(KERN_ERR "%s: timeout waiting for loader\n",
  112. __func__);
  113. ret = -EIO;
  114. goto out_drop_priv;
  115. }
  116. cond_resched();
  117. }
  118. if (!(status & SPU_STATUS_RUNNING)) {
  119. /* If isolated LOAD has failed: run SPU, we will get a stop-and
  120. * signal later. */
  121. pr_debug("%s: isolated LOAD failed\n", __func__);
  122. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  123. ret = -EACCES;
  124. goto out_drop_priv;
  125. }
  126. if (!(status & SPU_STATUS_ISOLATED_STATE)) {
  127. /* This isn't allowed by the CBEA, but check anyway */
  128. pr_debug("%s: SPU fell out of isolated mode?\n", __func__);
  129. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
  130. ret = -EINVAL;
  131. goto out_drop_priv;
  132. }
  133. out_drop_priv:
  134. /* Finished accessing the loader. Drop kernel mode */
  135. sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
  136. spu_mfc_sr1_set(ctx->spu, sr1);
  137. out:
  138. return ret;
  139. }
  140. static int spu_run_init(struct spu_context *ctx, u32 *npc)
  141. {
  142. unsigned long runcntl = SPU_RUNCNTL_RUNNABLE;
  143. int ret;
  144. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  145. /*
  146. * NOSCHED is synchronous scheduling with respect to the caller.
  147. * The caller waits for the context to be loaded.
  148. */
  149. if (ctx->flags & SPU_CREATE_NOSCHED) {
  150. if (ctx->state == SPU_STATE_SAVED) {
  151. ret = spu_activate(ctx, 0);
  152. if (ret)
  153. return ret;
  154. }
  155. }
  156. /*
  157. * Apply special setup as required.
  158. */
  159. if (ctx->flags & SPU_CREATE_ISOLATE) {
  160. if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
  161. ret = spu_setup_isolated(ctx);
  162. if (ret)
  163. return ret;
  164. }
  165. /*
  166. * If userspace has set the runcntrl register (eg, to
  167. * issue an isolated exit), we need to re-set it here
  168. */
  169. runcntl = ctx->ops->runcntl_read(ctx) &
  170. (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  171. if (runcntl == 0)
  172. runcntl = SPU_RUNCNTL_RUNNABLE;
  173. }
  174. if (ctx->flags & SPU_CREATE_NOSCHED) {
  175. spuctx_switch_state(ctx, SPU_UTIL_USER);
  176. ctx->ops->runcntl_write(ctx, runcntl);
  177. } else {
  178. unsigned long privcntl;
  179. if (test_thread_flag(TIF_SINGLESTEP))
  180. privcntl = SPU_PRIVCNTL_MODE_SINGLE_STEP;
  181. else
  182. privcntl = SPU_PRIVCNTL_MODE_NORMAL;
  183. ctx->ops->npc_write(ctx, *npc);
  184. ctx->ops->privcntl_write(ctx, privcntl);
  185. ctx->ops->runcntl_write(ctx, runcntl);
  186. if (ctx->state == SPU_STATE_SAVED) {
  187. ret = spu_activate(ctx, 0);
  188. if (ret)
  189. return ret;
  190. } else {
  191. spuctx_switch_state(ctx, SPU_UTIL_USER);
  192. }
  193. }
  194. set_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags);
  195. return 0;
  196. }
  197. static int spu_run_fini(struct spu_context *ctx, u32 *npc,
  198. u32 *status)
  199. {
  200. int ret = 0;
  201. spu_del_from_rq(ctx);
  202. *status = ctx->ops->status_read(ctx);
  203. *npc = ctx->ops->npc_read(ctx);
  204. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  205. clear_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags);
  206. spu_release(ctx);
  207. if (signal_pending(current))
  208. ret = -ERESTARTSYS;
  209. return ret;
  210. }
  211. /*
  212. * SPU syscall restarting is tricky because we violate the basic
  213. * assumption that the signal handler is running on the interrupted
  214. * thread. Here instead, the handler runs on PowerPC user space code,
  215. * while the syscall was called from the SPU.
  216. * This means we can only do a very rough approximation of POSIX
  217. * signal semantics.
  218. */
  219. static int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
  220. unsigned int *npc)
  221. {
  222. int ret;
  223. switch (*spu_ret) {
  224. case -ERESTARTSYS:
  225. case -ERESTARTNOINTR:
  226. /*
  227. * Enter the regular syscall restarting for
  228. * sys_spu_run, then restart the SPU syscall
  229. * callback.
  230. */
  231. *npc -= 8;
  232. ret = -ERESTARTSYS;
  233. break;
  234. case -ERESTARTNOHAND:
  235. case -ERESTART_RESTARTBLOCK:
  236. /*
  237. * Restart block is too hard for now, just return -EINTR
  238. * to the SPU.
  239. * ERESTARTNOHAND comes from sys_pause, we also return
  240. * -EINTR from there.
  241. * Assume that we need to be restarted ourselves though.
  242. */
  243. *spu_ret = -EINTR;
  244. ret = -ERESTARTSYS;
  245. break;
  246. default:
  247. printk(KERN_WARNING "%s: unexpected return code %ld\n",
  248. __func__, *spu_ret);
  249. ret = 0;
  250. }
  251. return ret;
  252. }
  253. static int spu_process_callback(struct spu_context *ctx)
  254. {
  255. struct spu_syscall_block s;
  256. u32 ls_pointer, npc;
  257. void __iomem *ls;
  258. long spu_ret;
  259. int ret;
  260. /* get syscall block from local store */
  261. npc = ctx->ops->npc_read(ctx) & ~3;
  262. ls = (void __iomem *)ctx->ops->get_ls(ctx);
  263. ls_pointer = in_be32(ls + npc);
  264. if (ls_pointer > (LS_SIZE - sizeof(s)))
  265. return -EFAULT;
  266. memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
  267. /* do actual syscall without pinning the spu */
  268. ret = 0;
  269. spu_ret = -ENOSYS;
  270. npc += 4;
  271. if (s.nr_ret < __NR_syscalls) {
  272. spu_release(ctx);
  273. /* do actual system call from here */
  274. spu_ret = spu_sys_callback(&s);
  275. if (spu_ret <= -ERESTARTSYS) {
  276. ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
  277. }
  278. mutex_lock(&ctx->state_mutex);
  279. if (ret == -ERESTARTSYS)
  280. return ret;
  281. }
  282. /* need to re-get the ls, as it may have changed when we released the
  283. * spu */
  284. ls = (void __iomem *)ctx->ops->get_ls(ctx);
  285. /* write result, jump over indirect pointer */
  286. memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
  287. ctx->ops->npc_write(ctx, npc);
  288. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  289. return ret;
  290. }
  291. long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event)
  292. {
  293. int ret;
  294. struct spu *spu;
  295. u32 status;
  296. if (mutex_lock_interruptible(&ctx->run_mutex))
  297. return -ERESTARTSYS;
  298. ctx->event_return = 0;
  299. ret = spu_acquire(ctx);
  300. if (ret)
  301. goto out_unlock;
  302. spu_enable_spu(ctx);
  303. spu_update_sched_info(ctx);
  304. ret = spu_run_init(ctx, npc);
  305. if (ret) {
  306. spu_release(ctx);
  307. goto out;
  308. }
  309. do {
  310. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
  311. if (unlikely(ret)) {
  312. /*
  313. * This is nasty: we need the state_mutex for all the
  314. * bookkeeping even if the syscall was interrupted by
  315. * a signal. ewww.
  316. */
  317. mutex_lock(&ctx->state_mutex);
  318. break;
  319. }
  320. spu = ctx->spu;
  321. if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE,
  322. &ctx->sched_flags))) {
  323. if (!(status & SPU_STATUS_STOPPED_BY_STOP)) {
  324. spu_switch_notify(spu, ctx);
  325. continue;
  326. }
  327. }
  328. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  329. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  330. (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
  331. ret = spu_process_callback(ctx);
  332. if (ret)
  333. break;
  334. status &= ~SPU_STATUS_STOPPED_BY_STOP;
  335. }
  336. ret = spufs_handle_class1(ctx);
  337. if (ret)
  338. break;
  339. ret = spufs_handle_class0(ctx);
  340. if (ret)
  341. break;
  342. if (signal_pending(current))
  343. ret = -ERESTARTSYS;
  344. } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
  345. SPU_STATUS_STOPPED_BY_HALT |
  346. SPU_STATUS_SINGLE_STEP)));
  347. spu_disable_spu(ctx);
  348. ret = spu_run_fini(ctx, npc, &status);
  349. spu_yield(ctx);
  350. spu_switch_log_notify(NULL, ctx, SWITCH_LOG_EXIT, status);
  351. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  352. (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100))
  353. ctx->stats.libassist++;
  354. if ((ret == 0) ||
  355. ((ret == -ERESTARTSYS) &&
  356. ((status & SPU_STATUS_STOPPED_BY_HALT) ||
  357. (status & SPU_STATUS_SINGLE_STEP) ||
  358. ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  359. (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
  360. ret = status;
  361. /* Note: we don't need to force_sig SIGTRAP on single-step
  362. * since we have TIF_SINGLESTEP set, thus the kernel will do
  363. * it upon return from the syscall anyawy
  364. */
  365. if (unlikely(status & SPU_STATUS_SINGLE_STEP))
  366. ret = -ERESTARTSYS;
  367. else if (unlikely((status & SPU_STATUS_STOPPED_BY_STOP)
  368. && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff)) {
  369. force_sig(SIGTRAP, current);
  370. ret = -ERESTARTSYS;
  371. }
  372. out:
  373. *event = ctx->event_return;
  374. out_unlock:
  375. mutex_unlock(&ctx->run_mutex);
  376. return ret;
  377. }