run.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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)
  11. {
  12. struct spu_context *ctx = spu->ctx;
  13. wake_up_all(&ctx->stop_wq);
  14. }
  15. static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
  16. {
  17. struct spu *spu;
  18. u64 pte_fault;
  19. *stat = ctx->ops->status_read(ctx);
  20. if (ctx->state != SPU_STATE_RUNNABLE)
  21. return 1;
  22. spu = ctx->spu;
  23. pte_fault = spu->dsisr &
  24. (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
  25. return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
  26. }
  27. static int spu_setup_isolated(struct spu_context *ctx)
  28. {
  29. int ret;
  30. u64 __iomem *mfc_cntl;
  31. u64 sr1;
  32. u32 status;
  33. unsigned long timeout;
  34. const u32 status_loading = SPU_STATUS_RUNNING
  35. | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
  36. ret = -ENODEV;
  37. if (!isolated_loader)
  38. goto out;
  39. /*
  40. * We need to exclude userspace access to the context.
  41. *
  42. * To protect against memory access we invalidate all ptes
  43. * and make sure the pagefault handlers block on the mutex.
  44. */
  45. spu_unmap_mappings(ctx);
  46. mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
  47. /* purge the MFC DMA queue to ensure no spurious accesses before we
  48. * enter kernel mode */
  49. timeout = jiffies + HZ;
  50. out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
  51. while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
  52. != MFC_CNTL_PURGE_DMA_COMPLETE) {
  53. if (time_after(jiffies, timeout)) {
  54. printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
  55. __FUNCTION__);
  56. ret = -EIO;
  57. goto out;
  58. }
  59. cond_resched();
  60. }
  61. /* put the SPE in kernel mode to allow access to the loader */
  62. sr1 = spu_mfc_sr1_get(ctx->spu);
  63. sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
  64. spu_mfc_sr1_set(ctx->spu, sr1);
  65. /* start the loader */
  66. ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
  67. ctx->ops->signal2_write(ctx,
  68. (unsigned long)isolated_loader & 0xffffffff);
  69. ctx->ops->runcntl_write(ctx,
  70. SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  71. ret = 0;
  72. timeout = jiffies + HZ;
  73. while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
  74. status_loading) {
  75. if (time_after(jiffies, timeout)) {
  76. printk(KERN_ERR "%s: timeout waiting for loader\n",
  77. __FUNCTION__);
  78. ret = -EIO;
  79. goto out_drop_priv;
  80. }
  81. cond_resched();
  82. }
  83. if (!(status & SPU_STATUS_RUNNING)) {
  84. /* If isolated LOAD has failed: run SPU, we will get a stop-and
  85. * signal later. */
  86. pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
  87. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  88. ret = -EACCES;
  89. goto out_drop_priv;
  90. }
  91. if (!(status & SPU_STATUS_ISOLATED_STATE)) {
  92. /* This isn't allowed by the CBEA, but check anyway */
  93. pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
  94. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
  95. ret = -EINVAL;
  96. goto out_drop_priv;
  97. }
  98. out_drop_priv:
  99. /* Finished accessing the loader. Drop kernel mode */
  100. sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
  101. spu_mfc_sr1_set(ctx->spu, sr1);
  102. out:
  103. return ret;
  104. }
  105. static inline int spu_run_init(struct spu_context *ctx, u32 * npc)
  106. {
  107. int ret;
  108. unsigned long runcntl = SPU_RUNCNTL_RUNNABLE;
  109. ret = spu_acquire_runnable(ctx, 0);
  110. if (ret)
  111. return ret;
  112. if (ctx->flags & SPU_CREATE_ISOLATE) {
  113. if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
  114. ret = spu_setup_isolated(ctx);
  115. if (ret)
  116. spu_release(ctx);
  117. }
  118. /* if userspace has set the runcntrl register (eg, to issue an
  119. * isolated exit), we need to re-set it here */
  120. runcntl = ctx->ops->runcntl_read(ctx) &
  121. (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  122. if (runcntl == 0)
  123. runcntl = SPU_RUNCNTL_RUNNABLE;
  124. } else {
  125. spu_start_tick(ctx);
  126. ctx->ops->npc_write(ctx, *npc);
  127. }
  128. ctx->ops->runcntl_write(ctx, runcntl);
  129. return ret;
  130. }
  131. static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
  132. u32 * status)
  133. {
  134. int ret = 0;
  135. spu_stop_tick(ctx);
  136. *status = ctx->ops->status_read(ctx);
  137. *npc = ctx->ops->npc_read(ctx);
  138. spu_release(ctx);
  139. if (signal_pending(current))
  140. ret = -ERESTARTSYS;
  141. return ret;
  142. }
  143. static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
  144. u32 *status)
  145. {
  146. int ret;
  147. if ((ret = spu_run_fini(ctx, npc, status)) != 0)
  148. return ret;
  149. if (*status & (SPU_STATUS_STOPPED_BY_STOP |
  150. SPU_STATUS_STOPPED_BY_HALT)) {
  151. return *status;
  152. }
  153. if ((ret = spu_run_init(ctx, npc)) != 0)
  154. return ret;
  155. return 0;
  156. }
  157. /*
  158. * SPU syscall restarting is tricky because we violate the basic
  159. * assumption that the signal handler is running on the interrupted
  160. * thread. Here instead, the handler runs on PowerPC user space code,
  161. * while the syscall was called from the SPU.
  162. * This means we can only do a very rough approximation of POSIX
  163. * signal semantics.
  164. */
  165. int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
  166. unsigned int *npc)
  167. {
  168. int ret;
  169. switch (*spu_ret) {
  170. case -ERESTARTSYS:
  171. case -ERESTARTNOINTR:
  172. /*
  173. * Enter the regular syscall restarting for
  174. * sys_spu_run, then restart the SPU syscall
  175. * callback.
  176. */
  177. *npc -= 8;
  178. ret = -ERESTARTSYS;
  179. break;
  180. case -ERESTARTNOHAND:
  181. case -ERESTART_RESTARTBLOCK:
  182. /*
  183. * Restart block is too hard for now, just return -EINTR
  184. * to the SPU.
  185. * ERESTARTNOHAND comes from sys_pause, we also return
  186. * -EINTR from there.
  187. * Assume that we need to be restarted ourselves though.
  188. */
  189. *spu_ret = -EINTR;
  190. ret = -ERESTARTSYS;
  191. break;
  192. default:
  193. printk(KERN_WARNING "%s: unexpected return code %ld\n",
  194. __FUNCTION__, *spu_ret);
  195. ret = 0;
  196. }
  197. return ret;
  198. }
  199. int spu_process_callback(struct spu_context *ctx)
  200. {
  201. struct spu_syscall_block s;
  202. u32 ls_pointer, npc;
  203. char *ls;
  204. long spu_ret;
  205. int ret;
  206. /* get syscall block from local store */
  207. npc = ctx->ops->npc_read(ctx);
  208. ls = ctx->ops->get_ls(ctx);
  209. ls_pointer = *(u32*)(ls + npc);
  210. if (ls_pointer > (LS_SIZE - sizeof(s)))
  211. return -EFAULT;
  212. memcpy(&s, ls + ls_pointer, sizeof (s));
  213. /* do actual syscall without pinning the spu */
  214. ret = 0;
  215. spu_ret = -ENOSYS;
  216. npc += 4;
  217. if (s.nr_ret < __NR_syscalls) {
  218. spu_release(ctx);
  219. /* do actual system call from here */
  220. spu_ret = spu_sys_callback(&s);
  221. if (spu_ret <= -ERESTARTSYS) {
  222. ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
  223. }
  224. spu_acquire(ctx);
  225. if (ret == -ERESTARTSYS)
  226. return ret;
  227. }
  228. /* write result, jump over indirect pointer */
  229. memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret));
  230. ctx->ops->npc_write(ctx, npc);
  231. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  232. return ret;
  233. }
  234. static inline int spu_process_events(struct spu_context *ctx)
  235. {
  236. struct spu *spu = ctx->spu;
  237. int ret = 0;
  238. if (spu->class_0_pending)
  239. ret = spu_irq_class_0_bottom(spu);
  240. if (!ret && signal_pending(current))
  241. ret = -ERESTARTSYS;
  242. return ret;
  243. }
  244. long spufs_run_spu(struct file *file, struct spu_context *ctx,
  245. u32 *npc, u32 *event)
  246. {
  247. int ret;
  248. u32 status;
  249. if (mutex_lock_interruptible(&ctx->run_mutex))
  250. return -ERESTARTSYS;
  251. ctx->ops->master_start(ctx);
  252. ctx->event_return = 0;
  253. ret = spu_run_init(ctx, npc);
  254. if (ret)
  255. goto out;
  256. do {
  257. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
  258. if (unlikely(ret))
  259. break;
  260. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  261. (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
  262. ret = spu_process_callback(ctx);
  263. if (ret)
  264. break;
  265. status &= ~SPU_STATUS_STOPPED_BY_STOP;
  266. }
  267. ret = spufs_handle_class1(ctx);
  268. if (ret)
  269. break;
  270. if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
  271. ret = spu_reacquire_runnable(ctx, npc, &status);
  272. if (ret) {
  273. spu_stop_tick(ctx);
  274. goto out2;
  275. }
  276. continue;
  277. }
  278. ret = spu_process_events(ctx);
  279. } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
  280. SPU_STATUS_STOPPED_BY_HALT)));
  281. ctx->ops->master_stop(ctx);
  282. ret = spu_run_fini(ctx, npc, &status);
  283. spu_yield(ctx);
  284. out2:
  285. if ((ret == 0) ||
  286. ((ret == -ERESTARTSYS) &&
  287. ((status & SPU_STATUS_STOPPED_BY_HALT) ||
  288. ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  289. (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
  290. ret = status;
  291. if ((status & SPU_STATUS_STOPPED_BY_STOP)
  292. && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
  293. force_sig(SIGTRAP, current);
  294. ret = -ERESTARTSYS;
  295. }
  296. out:
  297. *event = ctx->event_return;
  298. mutex_unlock(&ctx->run_mutex);
  299. return ret;
  300. }