run.c 11 KB

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