run.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <linux/wait.h>
  2. #include <linux/ptrace.h>
  3. #include <asm/spu.h>
  4. #include "spufs.h"
  5. /* interrupt-level stop callback function. */
  6. void spufs_stop_callback(struct spu *spu)
  7. {
  8. struct spu_context *ctx = spu->ctx;
  9. wake_up_all(&ctx->stop_wq);
  10. }
  11. static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
  12. {
  13. struct spu *spu;
  14. u64 pte_fault;
  15. *stat = ctx->ops->status_read(ctx);
  16. if (ctx->state != SPU_STATE_RUNNABLE)
  17. return 1;
  18. spu = ctx->spu;
  19. pte_fault = spu->dsisr &
  20. (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
  21. return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
  22. }
  23. static inline int spu_run_init(struct spu_context *ctx, u32 * npc,
  24. u32 * status)
  25. {
  26. int ret;
  27. if ((ret = spu_acquire_runnable(ctx)) != 0)
  28. return ret;
  29. ctx->ops->npc_write(ctx, *npc);
  30. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  31. return 0;
  32. }
  33. static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
  34. u32 * status)
  35. {
  36. int ret = 0;
  37. *status = ctx->ops->status_read(ctx);
  38. *npc = ctx->ops->npc_read(ctx);
  39. spu_release(ctx);
  40. if (signal_pending(current))
  41. ret = -ERESTARTSYS;
  42. if (unlikely(current->ptrace & PT_PTRACED)) {
  43. if ((*status & SPU_STATUS_STOPPED_BY_STOP)
  44. && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
  45. force_sig(SIGTRAP, current);
  46. ret = -ERESTARTSYS;
  47. }
  48. }
  49. return ret;
  50. }
  51. static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
  52. u32 *status)
  53. {
  54. int ret;
  55. if ((ret = spu_run_fini(ctx, npc, status)) != 0)
  56. return ret;
  57. if (*status & (SPU_STATUS_STOPPED_BY_STOP |
  58. SPU_STATUS_STOPPED_BY_HALT)) {
  59. return *status;
  60. }
  61. if ((ret = spu_run_init(ctx, npc, status)) != 0)
  62. return ret;
  63. return 0;
  64. }
  65. static inline int spu_process_events(struct spu_context *ctx)
  66. {
  67. struct spu *spu = ctx->spu;
  68. u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
  69. int ret = 0;
  70. if (spu->dsisr & pte_fault)
  71. ret = spu_irq_class_1_bottom(spu);
  72. if (spu->class_0_pending)
  73. ret = spu_irq_class_0_bottom(spu);
  74. if (!ret && signal_pending(current))
  75. ret = -ERESTARTSYS;
  76. return ret;
  77. }
  78. long spufs_run_spu(struct file *file, struct spu_context *ctx,
  79. u32 * npc, u32 * status)
  80. {
  81. int ret;
  82. if (down_interruptible(&ctx->run_sema))
  83. return -ERESTARTSYS;
  84. ret = spu_run_init(ctx, npc, status);
  85. if (ret)
  86. goto out;
  87. do {
  88. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
  89. if (unlikely(ret))
  90. break;
  91. if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
  92. ret = spu_reacquire_runnable(ctx, npc, status);
  93. if (ret)
  94. goto out;
  95. continue;
  96. }
  97. ret = spu_process_events(ctx);
  98. } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
  99. SPU_STATUS_STOPPED_BY_HALT)));
  100. ctx->ops->runcntl_stop(ctx);
  101. ret = spu_run_fini(ctx, npc, status);
  102. if (!ret)
  103. ret = *status;
  104. spu_yield(ctx);
  105. out:
  106. up(&ctx->run_sema);
  107. return ret;
  108. }