run.c 8.6 KB

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