run.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 int spu_run_init(struct spu_context *ctx, u32 * npc)
  106. {
  107. if (ctx->flags & SPU_CREATE_ISOLATE) {
  108. unsigned long runcntl;
  109. if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
  110. int ret = spu_setup_isolated(ctx);
  111. if (ret)
  112. return ret;
  113. }
  114. /* if userspace has set the runcntrl register (eg, to issue an
  115. * isolated exit), we need to re-set it here */
  116. runcntl = ctx->ops->runcntl_read(ctx) &
  117. (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  118. if (runcntl == 0)
  119. runcntl = SPU_RUNCNTL_RUNNABLE;
  120. ctx->ops->runcntl_write(ctx, runcntl);
  121. } else {
  122. spu_start_tick(ctx);
  123. ctx->ops->npc_write(ctx, *npc);
  124. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  125. }
  126. return 0;
  127. }
  128. static int spu_run_fini(struct spu_context *ctx, u32 * npc,
  129. u32 * status)
  130. {
  131. int ret = 0;
  132. spu_stop_tick(ctx);
  133. *status = ctx->ops->status_read(ctx);
  134. *npc = ctx->ops->npc_read(ctx);
  135. spu_release(ctx);
  136. if (signal_pending(current))
  137. ret = -ERESTARTSYS;
  138. return ret;
  139. }
  140. static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
  141. u32 *status)
  142. {
  143. int ret;
  144. ret = spu_run_fini(ctx, npc, status);
  145. if (ret)
  146. return ret;
  147. if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT))
  148. return *status;
  149. ret = spu_acquire_runnable(ctx, 0);
  150. if (ret)
  151. return ret;
  152. ret = spu_run_init(ctx, npc);
  153. if (ret) {
  154. spu_release(ctx);
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. /*
  160. * SPU syscall restarting is tricky because we violate the basic
  161. * assumption that the signal handler is running on the interrupted
  162. * thread. Here instead, the handler runs on PowerPC user space code,
  163. * while the syscall was called from the SPU.
  164. * This means we can only do a very rough approximation of POSIX
  165. * signal semantics.
  166. */
  167. int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
  168. unsigned int *npc)
  169. {
  170. int ret;
  171. switch (*spu_ret) {
  172. case -ERESTARTSYS:
  173. case -ERESTARTNOINTR:
  174. /*
  175. * Enter the regular syscall restarting for
  176. * sys_spu_run, then restart the SPU syscall
  177. * callback.
  178. */
  179. *npc -= 8;
  180. ret = -ERESTARTSYS;
  181. break;
  182. case -ERESTARTNOHAND:
  183. case -ERESTART_RESTARTBLOCK:
  184. /*
  185. * Restart block is too hard for now, just return -EINTR
  186. * to the SPU.
  187. * ERESTARTNOHAND comes from sys_pause, we also return
  188. * -EINTR from there.
  189. * Assume that we need to be restarted ourselves though.
  190. */
  191. *spu_ret = -EINTR;
  192. ret = -ERESTARTSYS;
  193. break;
  194. default:
  195. printk(KERN_WARNING "%s: unexpected return code %ld\n",
  196. __FUNCTION__, *spu_ret);
  197. ret = 0;
  198. }
  199. return ret;
  200. }
  201. int spu_process_callback(struct spu_context *ctx)
  202. {
  203. struct spu_syscall_block s;
  204. u32 ls_pointer, npc;
  205. void __iomem *ls;
  206. long spu_ret;
  207. int ret;
  208. /* get syscall block from local store */
  209. npc = ctx->ops->npc_read(ctx) & ~3;
  210. ls = (void __iomem *)ctx->ops->get_ls(ctx);
  211. ls_pointer = in_be32(ls + npc);
  212. if (ls_pointer > (LS_SIZE - sizeof(s)))
  213. return -EFAULT;
  214. memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
  215. /* do actual syscall without pinning the spu */
  216. ret = 0;
  217. spu_ret = -ENOSYS;
  218. npc += 4;
  219. if (s.nr_ret < __NR_syscalls) {
  220. spu_release(ctx);
  221. /* do actual system call from here */
  222. spu_ret = spu_sys_callback(&s);
  223. if (spu_ret <= -ERESTARTSYS) {
  224. ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
  225. }
  226. spu_acquire(ctx);
  227. if (ret == -ERESTARTSYS)
  228. return ret;
  229. }
  230. /* write result, jump over indirect pointer */
  231. memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
  232. ctx->ops->npc_write(ctx, npc);
  233. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  234. return ret;
  235. }
  236. static inline int spu_process_events(struct spu_context *ctx)
  237. {
  238. struct spu *spu = ctx->spu;
  239. int ret = 0;
  240. if (spu->class_0_pending)
  241. ret = spu_irq_class_0_bottom(spu);
  242. if (!ret && signal_pending(current))
  243. ret = -ERESTARTSYS;
  244. return ret;
  245. }
  246. long spufs_run_spu(struct file *file, struct spu_context *ctx,
  247. u32 *npc, u32 *event)
  248. {
  249. int ret;
  250. u32 status;
  251. if (mutex_lock_interruptible(&ctx->run_mutex))
  252. return -ERESTARTSYS;
  253. ctx->ops->master_start(ctx);
  254. ctx->event_return = 0;
  255. ret = spu_acquire_runnable(ctx, 0);
  256. if (ret)
  257. return ret;
  258. ret = spu_run_init(ctx, npc);
  259. if (ret) {
  260. spu_release(ctx);
  261. goto out;
  262. }
  263. do {
  264. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
  265. if (unlikely(ret))
  266. break;
  267. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  268. (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
  269. ret = spu_process_callback(ctx);
  270. if (ret)
  271. break;
  272. status &= ~SPU_STATUS_STOPPED_BY_STOP;
  273. }
  274. ret = spufs_handle_class1(ctx);
  275. if (ret)
  276. break;
  277. if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
  278. ret = spu_reacquire_runnable(ctx, npc, &status);
  279. if (ret) {
  280. spu_stop_tick(ctx);
  281. goto out2;
  282. }
  283. continue;
  284. }
  285. ret = spu_process_events(ctx);
  286. } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
  287. SPU_STATUS_STOPPED_BY_HALT)));
  288. ctx->ops->master_stop(ctx);
  289. ret = spu_run_fini(ctx, npc, &status);
  290. spu_yield(ctx);
  291. out2:
  292. if ((ret == 0) ||
  293. ((ret == -ERESTARTSYS) &&
  294. ((status & SPU_STATUS_STOPPED_BY_HALT) ||
  295. ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  296. (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
  297. ret = status;
  298. if ((status & SPU_STATUS_STOPPED_BY_STOP)
  299. && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
  300. force_sig(SIGTRAP, current);
  301. ret = -ERESTARTSYS;
  302. }
  303. out:
  304. *event = ctx->event_return;
  305. mutex_unlock(&ctx->run_mutex);
  306. return ret;
  307. }