run.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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(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, SPU_ACTIVATE_NOWAKE);
  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, SPU_ACTIVATE_NOWAKE);
  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. spu_start_tick(ctx);
  143. ctx->ops->npc_write(ctx, *npc);
  144. }
  145. ctx->ops->runcntl_write(ctx, runcntl);
  146. return ret;
  147. }
  148. static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
  149. u32 * status)
  150. {
  151. int ret = 0;
  152. spu_stop_tick(ctx);
  153. *status = ctx->ops->status_read(ctx);
  154. *npc = ctx->ops->npc_read(ctx);
  155. spu_release(ctx);
  156. if (signal_pending(current))
  157. ret = -ERESTARTSYS;
  158. return ret;
  159. }
  160. static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
  161. u32 *status)
  162. {
  163. int ret;
  164. if ((ret = spu_run_fini(ctx, npc, status)) != 0)
  165. return ret;
  166. if (*status & (SPU_STATUS_STOPPED_BY_STOP |
  167. SPU_STATUS_STOPPED_BY_HALT)) {
  168. return *status;
  169. }
  170. if ((ret = spu_run_init(ctx, npc)) != 0)
  171. return ret;
  172. return 0;
  173. }
  174. /*
  175. * SPU syscall restarting is tricky because we violate the basic
  176. * assumption that the signal handler is running on the interrupted
  177. * thread. Here instead, the handler runs on PowerPC user space code,
  178. * while the syscall was called from the SPU.
  179. * This means we can only do a very rough approximation of POSIX
  180. * signal semantics.
  181. */
  182. int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
  183. unsigned int *npc)
  184. {
  185. int ret;
  186. switch (*spu_ret) {
  187. case -ERESTARTSYS:
  188. case -ERESTARTNOINTR:
  189. /*
  190. * Enter the regular syscall restarting for
  191. * sys_spu_run, then restart the SPU syscall
  192. * callback.
  193. */
  194. *npc -= 8;
  195. ret = -ERESTARTSYS;
  196. break;
  197. case -ERESTARTNOHAND:
  198. case -ERESTART_RESTARTBLOCK:
  199. /*
  200. * Restart block is too hard for now, just return -EINTR
  201. * to the SPU.
  202. * ERESTARTNOHAND comes from sys_pause, we also return
  203. * -EINTR from there.
  204. * Assume that we need to be restarted ourselves though.
  205. */
  206. *spu_ret = -EINTR;
  207. ret = -ERESTARTSYS;
  208. break;
  209. default:
  210. printk(KERN_WARNING "%s: unexpected return code %ld\n",
  211. __FUNCTION__, *spu_ret);
  212. ret = 0;
  213. }
  214. return ret;
  215. }
  216. int spu_process_callback(struct spu_context *ctx)
  217. {
  218. struct spu_syscall_block s;
  219. u32 ls_pointer, npc;
  220. char *ls;
  221. long spu_ret;
  222. int ret;
  223. /* get syscall block from local store */
  224. npc = ctx->ops->npc_read(ctx);
  225. ls = ctx->ops->get_ls(ctx);
  226. ls_pointer = *(u32*)(ls + npc);
  227. if (ls_pointer > (LS_SIZE - sizeof(s)))
  228. return -EFAULT;
  229. memcpy(&s, ls + ls_pointer, sizeof (s));
  230. /* do actual syscall without pinning the spu */
  231. ret = 0;
  232. spu_ret = -ENOSYS;
  233. npc += 4;
  234. if (s.nr_ret < __NR_syscalls) {
  235. spu_release(ctx);
  236. /* do actual system call from here */
  237. spu_ret = spu_sys_callback(&s);
  238. if (spu_ret <= -ERESTARTSYS) {
  239. ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
  240. }
  241. spu_acquire(ctx);
  242. if (ret == -ERESTARTSYS)
  243. return ret;
  244. }
  245. /* write result, jump over indirect pointer */
  246. memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret));
  247. ctx->ops->npc_write(ctx, npc);
  248. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  249. return ret;
  250. }
  251. static inline int spu_process_events(struct spu_context *ctx)
  252. {
  253. struct spu *spu = ctx->spu;
  254. u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
  255. int ret = 0;
  256. if (spu->dsisr & pte_fault)
  257. ret = spu_irq_class_1_bottom(spu);
  258. if (spu->class_0_pending)
  259. ret = spu_irq_class_0_bottom(spu);
  260. if (!ret && signal_pending(current))
  261. ret = -ERESTARTSYS;
  262. return ret;
  263. }
  264. long spufs_run_spu(struct file *file, struct spu_context *ctx,
  265. u32 *npc, u32 *event)
  266. {
  267. int ret;
  268. u32 status;
  269. if (down_interruptible(&ctx->run_sema))
  270. return -ERESTARTSYS;
  271. ctx->ops->master_start(ctx);
  272. ctx->event_return = 0;
  273. ret = spu_run_init(ctx, npc);
  274. if (ret)
  275. goto out;
  276. do {
  277. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
  278. if (unlikely(ret))
  279. break;
  280. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  281. (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
  282. ret = spu_process_callback(ctx);
  283. if (ret)
  284. break;
  285. status &= ~SPU_STATUS_STOPPED_BY_STOP;
  286. }
  287. if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
  288. ret = spu_reacquire_runnable(ctx, npc, &status);
  289. if (ret) {
  290. spu_stop_tick(ctx);
  291. goto out2;
  292. }
  293. continue;
  294. }
  295. ret = spu_process_events(ctx);
  296. } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
  297. SPU_STATUS_STOPPED_BY_HALT)));
  298. ctx->ops->master_stop(ctx);
  299. ret = spu_run_fini(ctx, npc, &status);
  300. spu_yield(ctx);
  301. out2:
  302. if ((ret == 0) ||
  303. ((ret == -ERESTARTSYS) &&
  304. ((status & SPU_STATUS_STOPPED_BY_HALT) ||
  305. ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  306. (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
  307. ret = status;
  308. if ((status & SPU_STATUS_STOPPED_BY_STOP)
  309. && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
  310. force_sig(SIGTRAP, current);
  311. ret = -ERESTARTSYS;
  312. }
  313. out:
  314. *event = ctx->event_return;
  315. up(&ctx->run_sema);
  316. return ret;
  317. }